blob: d95139f8d385a26f0fd5f41d038a8be89ce68671 [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
Harald Welte9f75c352010-04-30 20:26:32 +020044#include <openbsc/signal.h>
45#include <openbsc/debug.h>
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020046#include <openbsc/gprs_gb_parse.h>
Harald Welte672f5c42010-05-03 18:54:58 +020047#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020048
Jacob Erlbeck67a44452014-05-19 10:14:58 +020049#include <openbsc/gprs_llc.h>
50#include <openbsc/gsm_04_08.h>
Harald Welte53373bc2016-04-20 17:11:43 +020051#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020052#include <openbsc/gprs_utils.h>
Jacob Erlbeck67a44452014-05-19 10:14:58 +020053
Daniel Willmann537d4802015-10-12 19:36:35 +020054#include <openssl/rand.h>
55
Jacob Erlbeckbc555742013-10-18 14:34:55 +020056static const struct rate_ctr_desc global_ctr_description[] = {
57 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020058 { "inv-lai", "Invalid Location Area Identifier" },
59 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020060 { "inv-nsei", "No BVC established for NSEI " },
61 { "proto-err.bss", "BSSGP protocol error (BSS )" },
62 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
63 { "not-supp.bss", "Feature not supported (BSS )" },
64 { "not-supp.sgsn", "Feature not supported (SGSN)" },
65 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
66 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
67 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020068 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020069};
70
71static const struct rate_ctr_group_desc global_ctrg_desc = {
72 .group_name_prefix = "gbproxy.global",
73 .group_description = "GBProxy Global Statistics",
74 .num_ctr = ARRAY_SIZE(global_ctr_description),
75 .ctr_desc = global_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010076 .class_id = OSMO_STATS_CLASS_GLOBAL,
Jacob Erlbeckbc555742013-10-18 14:34:55 +020077};
78
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020079static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
80 uint16_t ns_bvci);
81static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +020082 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +020083static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020084
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +020085static int check_peer_nsei(struct gbproxy_peer *peer, uint16_t nsei)
Jacob Erlbeckbc555742013-10-18 14:34:55 +020086{
87 if (peer->nsei != nsei) {
88 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
89 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
90 peer->bvci, nsei, peer->nsei);
91 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020092 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020093 }
94
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020095 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020096}
97
Harald Welte69619e32010-05-03 19:05:10 +020098/* strip off the NS header */
99static void strip_ns_hdr(struct msgb *msg)
100{
101 int strip_len = msgb_bssgph(msg) - msg->data;
102 msgb_pull(msg, strip_len);
103}
104
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200105/* Transmit Chapter 9.2.10 Identity Request */
106static void gprs_put_identity_req(struct msgb *msg, uint8_t id_type)
107{
108 struct gsm48_hdr *gh;
109
110 id_type &= GSM_MI_TYPE_MASK;
111
112 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
113 gh->proto_discr = GSM48_PDISC_MM_GPRS;
114 gh->msg_type = GSM48_MT_GMM_ID_REQ;
115 gh->data[0] = id_type;
116}
117
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200118/* Transmit Chapter 9.4.6.2 Detach Accept (mobile originated detach) */
119static void gprs_put_mo_detach_acc(struct msgb *msg)
120{
121 struct gsm48_hdr *gh;
122
123 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
124 gh->proto_discr = GSM48_PDISC_MM_GPRS;
125 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
126 gh->data[0] = 0; /* no force to standby */
127}
128
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200129static void gprs_push_llc_ui(struct msgb *msg,
130 int is_uplink, unsigned sapi, unsigned nu)
131{
132 const uint8_t e_bit = 0;
133 const uint8_t pm_bit = 1;
134 const uint8_t cr_bit = is_uplink ? 0 : 1;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200135 uint8_t *llc;
136 uint8_t *fcs_field;
137 uint32_t fcs;
138
139 nu &= 0x01ff; /* 9 Bit */
140
141 llc = msgb_push(msg, 3);
142 llc[0] = (cr_bit << 6) | (sapi & 0x0f);
143 llc[1] = 0xc0 | (nu >> 6); /* UI frame */
144 llc[2] = (nu << 2) | ((e_bit & 1) << 1) | (pm_bit & 1);
145
146 fcs = gprs_llc_fcs(llc, msgb_length(msg));
147 fcs_field = msgb_put(msg, 3);
148 fcs_field[0] = (uint8_t)(fcs >> 0);
149 fcs_field[1] = (uint8_t)(fcs >> 8);
150 fcs_field[2] = (uint8_t)(fcs >> 16);
151}
152
153static void gprs_push_bssgp_dl_unitdata(struct msgb *msg,
154 uint32_t tlli)
155{
156 struct bssgp_ud_hdr *budh;
157 uint8_t *llc = msgb_data(msg);
158 size_t llc_size = msgb_length(msg);
159 const size_t llc_ie_hdr_size = 3;
160 const uint8_t qos_profile[] = {0x00, 0x50, 0x20}; /* hard-coded */
161 const uint8_t lifetime[] = {0x02, 0x58}; /* 6s hard-coded */
162
163 const size_t bssgp_overhead = sizeof(*budh) +
164 TVLV_GROSS_LEN(sizeof(lifetime)) + llc_ie_hdr_size;
165 uint8_t *ie;
166 uint32_t tlli_be = htonl(tlli);
167
168 budh = (struct bssgp_ud_hdr *)msgb_push(msg, bssgp_overhead);
169
170 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
171 memcpy(&budh->tlli, &tlli_be, sizeof(budh->tlli));
172 memcpy(&budh->qos_profile, qos_profile, sizeof(budh->qos_profile));
173
174 ie = budh->data;
175 tvlv_put(ie, BSSGP_IE_PDU_LIFETIME, sizeof(lifetime), lifetime);
176 ie += TVLV_GROSS_LEN(sizeof(lifetime));
177
178 /* Note: Add alignment before the LLC IE if inserting other IE */
179
180 *(ie++) = BSSGP_IE_LLC_PDU;
181 *(ie++) = llc_size / 256;
182 *(ie++) = llc_size % 256;
183
184 OSMO_ASSERT(ie == llc);
185
186 msgb_bssgph(msg) = (uint8_t *)budh;
187 msgb_tlli(msg) = tlli;
188}
189
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200190/* update peer according to the BSS message */
191static void gbprox_update_current_raid(uint8_t *raid_enc,
192 struct gbproxy_peer *peer,
193 const char *log_text)
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200194{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200195 struct gbproxy_patch_state *state = &peer->patch_state;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200196 const int old_local_mcc = state->local_mcc;
197 const int old_local_mnc = state->local_mnc;
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200198 struct gprs_ra_id raid;
199
200 if (!raid_enc)
201 return;
202
203 gsm48_parse_ra(&raid, raid_enc);
204
205 /* save source side MCC/MNC */
206 if (!peer->cfg->core_mcc || raid.mcc == peer->cfg->core_mcc) {
207 state->local_mcc = 0;
208 } else {
209 state->local_mcc = raid.mcc;
210 }
211
212 if (!peer->cfg->core_mnc || raid.mnc == peer->cfg->core_mnc) {
213 state->local_mnc = 0;
214 } else {
215 state->local_mnc = raid.mnc;
216 }
217
218 if (old_local_mcc != state->local_mcc ||
219 old_local_mnc != state->local_mnc)
220 LOGP(DGPRS, LOGL_NOTICE,
221 "Patching RAID %sactivated, msg: %s, "
222 "local: %d-%d, core: %d-%d\n",
223 state->local_mcc || state->local_mnc ?
224 "" : "de",
225 log_text,
226 state->local_mcc, state->local_mnc,
227 peer->cfg->core_mcc, peer->cfg->core_mnc);
228}
229
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200230uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer,
231 uint32_t sgsn_ptmsi)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200232{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200233 uint32_t bss_ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200234 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200235 if (!peer->cfg->patch_ptmsi) {
236 bss_ptmsi = sgsn_ptmsi;
237 } else {
238 do {
Daniel Willmann537d4802015-10-12 19:36:35 +0200239 if (RAND_bytes((uint8_t *) &bss_ptmsi, sizeof(bss_ptmsi)) != 1) {
240 bss_ptmsi = GSM_RESERVED_TMSI;
241 break;
242 }
243
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200244 bss_ptmsi = bss_ptmsi | 0xC0000000;
245
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200246 if (gbproxy_link_info_by_ptmsi(peer, bss_ptmsi))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200247 bss_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200248 } while (bss_ptmsi == GSM_RESERVED_TMSI && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200249 }
250
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200251 if (bss_ptmsi == GSM_RESERVED_TMSI)
252 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a BSS P-TMSI\n");
253
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200254 return bss_ptmsi;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200255}
256
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200257uint32_t gbproxy_make_sgsn_tlli(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200258 struct gbproxy_link_info *link_info,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200259 uint32_t bss_tlli)
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200260{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200261 uint32_t sgsn_tlli;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200262 int max_retries = 23;
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200263 if (!peer->cfg->patch_ptmsi) {
264 sgsn_tlli = bss_tlli;
Jacob Erlbeck41108682014-10-31 10:47:29 +0100265 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
266 gprs_tlli_type(bss_tlli) == TLLI_FOREIGN) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200267 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200268 TLLI_FOREIGN);
Jacob Erlbeck41108682014-10-31 10:47:29 +0100269 } else if (link_info->sgsn_tlli.ptmsi != GSM_RESERVED_TMSI &&
270 gprs_tlli_type(bss_tlli) == TLLI_LOCAL) {
271 sgsn_tlli = gprs_tmsi2tlli(link_info->sgsn_tlli.ptmsi,
272 TLLI_LOCAL);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200273 } else {
274 do {
275 /* create random TLLI, 0b01111xxx... */
Daniel Willmann537d4802015-10-12 19:36:35 +0200276 if (RAND_bytes((uint8_t *) &sgsn_tlli, sizeof(sgsn_tlli)) != 1) {
277 sgsn_tlli = 0;
278 break;
279 }
280
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200281 sgsn_tlli = (sgsn_tlli & 0x7fffffff) | 0x78000000;
282
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200283 if (gbproxy_link_info_by_any_sgsn_tlli(peer, sgsn_tlli))
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200284 sgsn_tlli = 0;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200285 } while (!sgsn_tlli && max_retries--);
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200286 }
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200287
288 if (!sgsn_tlli)
289 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate an SGSN TLLI\n");
290
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200291 return sgsn_tlli;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200292}
293
Jacob Erlbeckd211d1d2014-09-22 13:30:46 +0200294void gbproxy_reset_link(struct gbproxy_link_info *link_info)
295{
296 gbproxy_reset_imsi_acquisition(link_info);
297}
298
Jacob Erlbeck31591142014-09-03 11:59:48 +0200299/* Returns != 0 iff IMSI acquisition was in progress */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200300static int gbproxy_restart_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200301{
302 int in_progress = 0;
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200303 if (!link_info)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200304 return 0;
305
Jacob Erlbeck65750282014-09-22 15:41:21 +0200306 if (link_info->imsi_acq_pending)
Jacob Erlbeck31591142014-09-03 11:59:48 +0200307 in_progress = 1;
308
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200309 gbproxy_link_info_discard_messages(link_info);
310 link_info->imsi_acq_pending = 0;
Jacob Erlbeck31591142014-09-03 11:59:48 +0200311
312 return in_progress;
313}
314
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200315static void gbproxy_reset_imsi_acquisition(struct gbproxy_link_info* link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200316{
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200317 gbproxy_restart_imsi_acquisition(link_info);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200318 link_info->vu_gen_tx_bss = GBPROXY_INIT_VU_GEN_TX;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200319}
320
Daniel Willmannbeade312016-11-07 17:54:29 +0100321static int gbproxy_flush_stored_messages(struct gbproxy_peer *peer,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200322 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200323 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200324 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200325 struct gprs_gb_parse_context *parse_ctx)
326{
327 int rc;
328 struct msgb *stored_msg;
329 /* Got identity response with IMSI, assuming the request had
330 * been generated by the gbproxy */
331
332 LOGP(DLLC, LOGL_DEBUG,
333 "NSEI=%d(BSS) IMSI acquisition succeeded, "
334 "flushing stored messages\n",
335 msgb_nsei(msg));
336
337 /* Patch and flush stored messages towards the SGSN */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200338 while ((stored_msg = msgb_dequeue(&link_info->stored_msgs))) {
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200339 struct gprs_gb_parse_context tmp_parse_ctx = {0};
340 tmp_parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200341 tmp_parse_ctx.peer_nsei = msgb_nsei(stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200342 int len_change = 0;
343
344 gprs_gb_parse_bssgp(msgb_bssgph(stored_msg),
345 msgb_bssgp_len(stored_msg),
346 &tmp_parse_ctx);
347 gbproxy_patch_bssgp(msg, msgb_bssgph(stored_msg),
348 msgb_bssgp_len(stored_msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200349 peer, link_info, &len_change,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200350 &tmp_parse_ctx);
351
Daniel Willmannbeade312016-11-07 17:54:29 +0100352 rc = gbproxy_update_link_state_after(peer, link_info, now,
353 &tmp_parse_ctx);
354 if (rc == 1) {
355 LOGP(DLLC, LOGL_NOTICE, "link_info deleted while flushing stored messages\n");
356 msgb_free(stored_msg);
357 return -1;
358 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200359
360 rc = gbprox_relay2sgsn(peer->cfg, stored_msg,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200361 msgb_bvci(msg), link_info->sgsn_nsei);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200362
363 if (rc < 0)
364 LOGP(DLLC, LOGL_ERROR,
365 "NSEI=%d(BSS) failed to send stored message "
366 "(%s)\n",
367 msgb_nsei(msg),
368 parse_ctx->llc_msg_name ?
369 parse_ctx->llc_msg_name : "BSSGP");
370 msgb_free(stored_msg);
371 }
Daniel Willmannbeade312016-11-07 17:54:29 +0100372
373 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200374}
375
Jacob Erlbeck65750282014-09-22 15:41:21 +0200376static int gbproxy_gsm48_to_peer(struct gbproxy_peer *peer,
377 struct gbproxy_link_info* link_info,
378 uint16_t bvci,
379 struct msgb *msg /* Takes msg ownership */)
380{
381 int rc;
382
383 /* Workaround to avoid N(U) collisions and to enable a restart
384 * of the IMSI acquisition procedure. This will work unless the
385 * SGSN has an initial V(UT) within [256-32, 256+n_retries]
386 * (see GSM 04.64, 8.4.2). */
387 gprs_push_llc_ui(msg, 0, GPRS_SAPI_GMM, link_info->vu_gen_tx_bss);
388 link_info->vu_gen_tx_bss = (link_info->vu_gen_tx_bss + 1) % 512;
389
390 gprs_push_bssgp_dl_unitdata(msg, link_info->tlli.current);
391 rc = gbprox_relay2peer(msg, peer, bvci);
392 msgb_free(msg);
393 return rc;
394}
395
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200396static void gbproxy_acquire_imsi(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200397 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200398 uint16_t bvci)
399{
400 struct msgb *idreq_msg;
401
402 /* Send IDENT REQ */
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100403 idreq_msg = gsm48_msgb_alloc_name("GSM 04.08 ACQ IMSI");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200404 gprs_put_identity_req(idreq_msg, GSM_MI_TYPE_IMSI);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200405 gbproxy_gsm48_to_peer(peer, link_info, bvci, idreq_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200406}
407
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200408static void gbproxy_tx_detach_acc(struct gbproxy_peer *peer,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200409 struct gbproxy_link_info* link_info,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200410 uint16_t bvci)
411{
412 struct msgb *detacc_msg;
413
414 /* Send DETACH ACC */
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100415 detacc_msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACC");
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200416 gprs_put_mo_detach_acc(detacc_msg);
Jacob Erlbeck65750282014-09-22 15:41:21 +0200417 gbproxy_gsm48_to_peer(peer, link_info, bvci, detacc_msg);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200418}
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200419
420/* Return != 0 iff msg still needs to be processed */
421static int gbproxy_imsi_acquisition(struct gbproxy_peer *peer,
422 struct msgb *msg,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200423 time_t now,
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200424 struct gbproxy_link_info* link_info,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200425 struct gprs_gb_parse_context *parse_ctx)
426{
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200427 struct msgb *stored_msg;
428
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200429 if (!link_info)
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200430 return 1;
431
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200432 if (!link_info->imsi_acq_pending && link_info->imsi_len > 0)
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200433 return 1;
434
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200435 if (parse_ctx->g48_hdr)
436 switch (parse_ctx->g48_hdr->msg_type)
437 {
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200438 case GSM48_MT_GMM_RA_UPD_REQ:
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200439 case GSM48_MT_GMM_ATTACH_REQ:
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200440 if (gbproxy_restart_imsi_acquisition(link_info)) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200441 LOGP(DLLC, LOGL_INFO,
442 "NSEI=%d(BSS) IMSI acquisition was in progress "
Jacob Erlbecke27ab912014-09-18 10:08:27 +0200443 "when receiving an %s.\n",
444 msgb_nsei(msg), parse_ctx->llc_msg_name);
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200445 }
446 break;
447 case GSM48_MT_GMM_DETACH_REQ:
448 /* Nothing has been sent to the SGSN yet */
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200449 if (link_info->imsi_acq_pending) {
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200450 LOGP(DLLC, LOGL_INFO,
451 "NSEI=%d(BSS) IMSI acquisition was in progress "
452 "when receiving a DETACH_REQ.\n",
453 msgb_nsei(msg));
454 }
455 if (!parse_ctx->invalidate_tlli) {
456 LOGP(DLLC, LOGL_INFO,
457 "NSEI=%d(BSS) IMSI not yet acquired, "
458 "faking a DETACH_ACC.\n",
459 msgb_nsei(msg));
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200460 gbproxy_tx_detach_acc(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200461 parse_ctx->invalidate_tlli = 1;
462 }
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200463 gbproxy_reset_imsi_acquisition(link_info);
464 gbproxy_update_link_state_after(peer, link_info, now,
Jacob Erlbeck2a5096d2014-09-12 12:12:31 +0200465 parse_ctx);
466 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200467 }
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200468
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200469 if (link_info->imsi_acq_pending && link_info->imsi_len > 0) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200470 int is_ident_resp =
471 parse_ctx->g48_hdr &&
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100472 gsm48_hdr_pdisc(parse_ctx->g48_hdr) == GSM48_PDISC_MM_GPRS &&
473 gsm48_hdr_msg_type(parse_ctx->g48_hdr) == GSM48_MT_GMM_ID_RESP;
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200474
Daniel Willmannbeade312016-11-07 17:54:29 +0100475 /* The IMSI is now available. If flushing the messages fails,
476 * then link_info has been deleted and we should return
477 * immediately. */
478 if (gbproxy_flush_stored_messages(peer, msg, now, link_info,
479 parse_ctx) < 0)
480 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200481
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200482 gbproxy_reset_imsi_acquisition(link_info);
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200483
484 /* This message is most probably the response to the ident
485 * request sent by gbproxy_acquire_imsi(). Don't forward it to
486 * the SGSN. */
487 return !is_ident_resp;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200488 }
489
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200490 /* The message cannot be processed since the IMSI is still missing */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200491
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200492 /* Enqueue unpatched messages */
493 LOGP(DLLC, LOGL_INFO,
494 "NSEI=%d(BSS) IMSI acquisition in progress, "
495 "storing message (%s)\n",
496 msgb_nsei(msg),
497 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200498
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200499 stored_msg = gprs_msgb_copy(msg, "process_bssgp_ul");
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200500 msgb_enqueue(&link_info->stored_msgs, stored_msg);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200501
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200502 if (!link_info->imsi_acq_pending) {
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200503 LOGP(DLLC, LOGL_INFO,
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200504 "NSEI=%d(BSS) IMSI is required but not available, "
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200505 "initiating identification procedure (%s)\n",
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200506 msgb_nsei(msg),
507 parse_ctx->llc_msg_name ? parse_ctx->llc_msg_name : "BSSGP");
508
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200509 gbproxy_acquire_imsi(peer, link_info, msgb_bvci(msg));
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200510
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200511 /* There is no explicit retransmission handling, the
512 * implementation relies on the MS doing proper retransmissions
513 * of the triggering message instead */
514
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200515 link_info->imsi_acq_pending = 1;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200516 }
517
Jacob Erlbeck6bafa4c2014-09-11 11:35:17 +0200518 return 0;
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200519}
520
521struct gbproxy_peer *gbproxy_find_peer(struct gbproxy_config *cfg,
522 struct msgb *msg,
523 struct gprs_gb_parse_context *parse_ctx)
524{
525 struct gbproxy_peer *peer = NULL;
526
527 if (msgb_bvci(msg) >= 2)
528 peer = gbproxy_peer_by_bvci(cfg, msgb_bvci(msg));
529
530 if (!peer && !parse_ctx->to_bss)
531 peer = gbproxy_peer_by_nsei(cfg, msgb_nsei(msg));
532
533 if (!peer)
534 peer = gbproxy_peer_by_bssgp_tlv(cfg, &parse_ctx->bssgp_tp);
535
536 if (!peer) {
537 LOGP(DLLC, LOGL_INFO,
538 "NSEI=%d(%s) patching: didn't find peer for message, "
539 "PDU %d\n",
540 msgb_nsei(msg), parse_ctx->to_bss ? "BSS" : "SGSN",
541 parse_ctx->pdu_type);
542 /* Increment counter */
543 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PATCH_PEER_ERR]);
544 }
545 return peer;
546}
547
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200548/* patch BSSGP message */
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200549static int gbprox_process_bssgp_ul(struct gbproxy_config *cfg,
550 struct msgb *msg,
551 struct gbproxy_peer *peer)
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200552{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200553 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200554 int rc;
Jacob Erlbeckc8128822014-08-07 16:07:24 +0200555 int len_change = 0;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200556 time_t now;
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200557 struct timespec ts = {0,};
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200558 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200559 uint32_t sgsn_nsei = cfg->nsip_sgsn_nsei;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200560
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200561 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200562 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200563 return 1;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200564
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200565 parse_ctx.to_bss = 0;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200566 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200567
568 /* Parse BSSGP/LLC */
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200569 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
570 &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200571
Jacob Erlbeckb4f0e802014-10-09 12:28:15 +0200572 if (!rc && !parse_ctx.need_decryption) {
573 LOGP(DGPRS, LOGL_ERROR,
574 "NSEI=%u(BSS) patching: failed to parse invalid %s message\n",
575 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
576 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
577 LOGP(DGPRS, LOGL_NOTICE,
578 "NSEI=%u(BSS) invalid message was: %s\n",
579 msgb_nsei(msg), msgb_hexdump(msg));
580 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200581 }
582
583 /* Get peer */
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200584 if (!peer)
585 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200586
587 if (!peer)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200588 return 0;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200589
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200590
591 clock_gettime(CLOCK_MONOTONIC, &ts);
592 now = ts.tv_sec;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200593
Jacob Erlbeck12496dd2014-08-18 15:41:41 +0200594 gbprox_update_current_raid(parse_ctx.bssgp_raid_enc, peer,
595 parse_ctx.llc_msg_name);
596
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200597 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck31591142014-09-03 11:59:48 +0200598
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200599 link_info = gbproxy_update_link_state_ul(peer, now, &parse_ctx);
Jacob Erlbeck31591142014-09-03 11:59:48 +0200600
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200601 if (parse_ctx.g48_hdr) {
602 switch (parse_ctx.g48_hdr->msg_type) {
603 case GSM48_MT_GMM_ATTACH_REQ:
604 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REQS]);
605 break;
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100606 case GSM48_MT_GMM_DETACH_REQ:
607 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_REQS]);
608 break;
609 case GSM48_MT_GMM_ATTACH_COMPL:
610 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_COMPLS]);
611 break;
612 case GSM48_MT_GMM_RA_UPD_REQ:
613 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REQS]);
614 break;
615 case GSM48_MT_GMM_RA_UPD_COMPL:
616 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_COMPLS]);
617 break;
618 case GSM48_MT_GMM_STATUS:
619 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_BSS]);
620 break;
621 case GSM48_MT_GSM_ACT_PDP_REQ:
622 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REQS]);
623 break;
624 case GSM48_MT_GSM_DEACT_PDP_REQ:
625 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_REQS]);
626 break;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200627
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200628 default:
629 break;
630 }
631 }
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200632
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200633 if (link_info && cfg->route_to_sgsn2) {
634 if (cfg->acquire_imsi && link_info->imsi_len == 0)
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200635 sgsn_nsei = 0xffff;
Jacob Erlbeckb36032c2014-09-25 13:21:48 +0200636 else if (gbproxy_imsi_matches(cfg, GBPROX_MATCH_ROUTING,
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200637 link_info))
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200638 sgsn_nsei = cfg->nsip_sgsn2_nsei;
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200639 }
640
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200641 if (link_info)
642 link_info->sgsn_nsei = sgsn_nsei;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200643
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200644 /* Handle IMSI acquisition */
645 if (cfg->acquire_imsi) {
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200646 rc = gbproxy_imsi_acquisition(peer, msg, now, link_info,
Jacob Erlbeck9a6b7632014-09-19 13:23:21 +0200647 &parse_ctx);
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200648 if (rc <= 0)
649 return rc;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200650 }
651
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200652 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200653 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200654
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200655 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200656
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200657 if (sgsn_nsei != cfg->nsip_sgsn_nsei) {
Jacob Erlbeckcabd24b2014-08-27 12:51:07 +0200658 /* Send message directly to the selected SGSN */
659 rc = gbprox_relay2sgsn(cfg, msg, msgb_bvci(msg), sgsn_nsei);
660 /* Don't let the calling code handle the transmission */
661 return 0;
662 }
663
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200664 return 1;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200665}
666
667/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
668static void gbprox_process_bssgp_dl(struct gbproxy_config *cfg,
669 struct msgb *msg,
670 struct gbproxy_peer *peer)
671{
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200672 struct gprs_gb_parse_context parse_ctx = {0};
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200673 int rc;
674 int len_change = 0;
675 time_t now;
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200676 struct timespec ts = {0,};
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200677 struct gbproxy_link_info *link_info = NULL;
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200678
Jacob Erlbeck1abfdc22014-09-04 11:42:08 +0200679 if (!cfg->core_mcc && !cfg->core_mnc && !cfg->core_apn &&
680 !cfg->acquire_imsi && !cfg->patch_ptmsi && !cfg->route_to_sgsn2)
681 return;
682
Jacob Erlbeck9057bc32014-08-12 16:30:30 +0200683 parse_ctx.to_bss = 1;
Jacob Erlbeck91a0e862014-09-17 10:56:38 +0200684 parse_ctx.peer_nsei = msgb_nsei(msg);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200685
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200686 rc = gprs_gb_parse_bssgp(msgb_bssgph(msg), msgb_bssgp_len(msg),
687 &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200688
Jacob Erlbeckb4f0e802014-10-09 12:28:15 +0200689 if (!rc && !parse_ctx.need_decryption) {
690 LOGP(DGPRS, LOGL_ERROR,
691 "NSEI=%u(SGSN) patching: failed to parse invalid %s message\n",
692 msgb_nsei(msg), gprs_gb_message_name(&parse_ctx, "NS_UNITDATA"));
693 gprs_gb_log_parse_context(LOGL_NOTICE, &parse_ctx, "NS_UNITDATA");
694 LOGP(DGPRS, LOGL_NOTICE,
695 "NSEI=%u(SGSN) invalid message was: %s\n",
696 msgb_nsei(msg), msgb_hexdump(msg));
697 return;
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200698 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200699
Jacob Erlbeck0b243a12014-09-11 10:30:21 +0200700 /* Get peer */
701 if (!peer)
702 peer = gbproxy_find_peer(cfg, msg, &parse_ctx);
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200703
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200704 if (!peer)
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200705 return;
Jacob Erlbeck006c0382014-05-27 13:49:04 +0200706
Jacob Erlbeckc6807c42014-09-19 16:34:01 +0200707 clock_gettime(CLOCK_MONOTONIC, &ts);
708 now = ts.tv_sec;
Jacob Erlbeck7b821d02014-08-08 08:37:37 +0200709
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200710 if (parse_ctx.g48_hdr) {
711 switch (parse_ctx.g48_hdr->msg_type) {
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100712 case GSM48_MT_GMM_ATTACH_ACK:
713 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_ACKS]);
714 break;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200715 case GSM48_MT_GMM_ATTACH_REJ:
716 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_ATTACH_REJS]);
717 break;
Holger Hans Peter Freyther98fa3dc2015-11-09 14:30:22 +0100718 case GSM48_MT_GMM_DETACH_ACK:
719 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DETACH_ACKS]);
720 break;
721 case GSM48_MT_GMM_RA_UPD_ACK:
722 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_ACKS]);
723 break;
724 case GSM48_MT_GMM_RA_UPD_REJ:
725 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_RA_UPD_REJS]);
726 break;
727 case GSM48_MT_GMM_STATUS:
728 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_GMM_STATUS_SGSN]);
729 break;
730 case GSM48_MT_GSM_ACT_PDP_ACK:
731 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_ACKS]);
732 break;
733 case GSM48_MT_GSM_ACT_PDP_REJ:
734 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_ACT_REJS]);
735 break;
736 case GSM48_MT_GSM_DEACT_PDP_ACK:
737 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_PDP_DEACT_ACKS]);
738 break;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200739
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200740 default:
741 break;
742 }
743 }
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200744
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200745 gprs_gb_log_parse_context(LOGL_DEBUG, &parse_ctx, "NS_UNITDATA");
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200746
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200747 link_info = gbproxy_update_link_state_dl(peer, now, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200748
749 gbproxy_patch_bssgp(msg, msgb_bssgph(msg), msgb_bssgp_len(msg),
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200750 peer, link_info, &len_change, &parse_ctx);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200751
Jacob Erlbeck91d2f8a2014-09-19 15:07:27 +0200752 gbproxy_update_link_state_after(peer, link_info, now, &parse_ctx);
Jacob Erlbeck2db25122014-08-06 12:41:31 +0200753
Jacob Erlbeck03551e52014-06-27 19:56:05 +0200754 return;
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200755}
756
Harald Welte9f75c352010-04-30 20:26:32 +0200757/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200758static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200759 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200760{
Harald Welte39d0bb52010-05-12 18:10:25 +0000761 /* create a copy of the message so the old one can
762 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200763 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200764 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000765
Harald Weltee9ea2692010-05-11 20:20:13 +0200766 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200767 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200768
Harald Welte672f5c42010-05-03 18:54:58 +0200769 msgb_bvci(msg) = ns_bvci;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200770 msgb_nsei(msg) = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200771
Harald Welte69619e32010-05-03 19:05:10 +0200772 strip_ns_hdr(msg);
773
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200774 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
775 if (rc < 0)
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200776 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200777
778 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200779}
780
Harald Welte672f5c42010-05-03 18:54:58 +0200781/* feed a message down the NS-VC associated with the specified peer */
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200782static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200783 uint16_t ns_bvci)
784{
Harald Welte39d0bb52010-05-12 18:10:25 +0000785 /* create a copy of the message so the old one can
786 * be free()d safely when we return from gbprox_rcvmsg() */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200787 struct msgb *msg = gprs_msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200788 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000789
Harald Welte0ab535b2010-05-13 10:34:56 +0200790 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200791 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200792
Harald Welte9f75c352010-04-30 20:26:32 +0200793 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200794 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200795
Harald Welte0ab535b2010-05-13 10:34:56 +0200796 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200797 strip_ns_hdr(msg);
798
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200799 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
800 if (rc < 0)
801 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
802
803 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200804}
805
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200806static int block_unblock_peer(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100807{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200808 struct gbproxy_peer *peer;
Harald Welte36f98d92011-02-06 13:09:29 +0100809
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200810 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100811 if (!peer) {
812 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
813 ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200814 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100815 return -ENOENT;
816 }
817
818 switch (pdu_type) {
819 case BSSGP_PDUT_BVC_BLOCK_ACK:
820 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200821 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100822 break;
823 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
824 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200825 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100826 break;
827 default:
828 break;
829 }
830 return 0;
831}
832
Harald Welte9f75c352010-04-30 20:26:32 +0200833/* Send a message to a peer identified by ptp_bvci but using ns_bvci
834 * in the NS hdr */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200835static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200836 uint16_t ns_bvci)
837{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200838 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +0200839
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200840 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200841 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200842 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200843 ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200844 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200845 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200846 }
Harald Welte9f75c352010-04-30 20:26:32 +0200847
Harald Welte69619e32010-05-03 19:05:10 +0200848 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200849}
850
Harald Welteb1fd9022012-06-17 12:16:31 +0800851int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
852{
853 return 0;
854}
855
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200856/* Receive an incoming PTP message from a BSS-side NS-VC */
857static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
858 struct msgb *msg, uint16_t nsei,
859 uint16_t nsvci, uint16_t ns_bvci)
860{
861 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200862 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
863 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200864 int rc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200865
866 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200867 if (!peer) {
868 LOGP(DGPRS, LOGL_NOTICE, "Didn't find peer for "
869 "BVCI=%u for PTP message from NSVC=%u/NSEI=%u (BSS), "
870 "discarding message\n",
871 ns_bvci, nsvci, nsei);
872 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
873 &ns_bvci, msg);
874 }
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200875
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200876 check_peer_nsei(peer, nsei);
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200877
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200878 rc = gbprox_process_bssgp_ul(cfg, msg, peer);
879 if (!rc)
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +0200880 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200881
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200882 switch (pdu_type) {
883 case BSSGP_PDUT_FLOW_CONTROL_BVC:
884 if (!cfg->route_to_sgsn2)
885 break;
886
887 /* Send a copy to the secondary SGSN */
888 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
889 break;
890 default:
891 break;
892 }
893
894
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200895 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200896}
897
898/* Receive an incoming PTP message from a SGSN-side NS-VC */
899static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
900 struct msgb *msg, uint16_t nsei,
901 uint16_t nsvci, uint16_t ns_bvci)
902{
903 struct gbproxy_peer *peer;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200904 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
905 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200906
907 peer = gbproxy_peer_by_bvci(cfg, ns_bvci);
908
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200909 /* Send status messages before patching */
910
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200911 if (!peer) {
912 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
913 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
914 ns_bvci, nsvci, nsei);
915 rate_ctr_inc(&cfg->ctrg->
916 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
917 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
918 &ns_bvci, msg);
919 }
920
921 if (peer->blocked) {
922 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
923 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
924 ns_bvci, nsvci, nsei);
925 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200926 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200927 }
928
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200929 switch (pdu_type) {
930 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
931 case BSSGP_PDUT_BVC_BLOCK_ACK:
932 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
933 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
934 /* Hide ACKs from the secondary SGSN, the primary SGSN
935 * is responsible to send them. */
936 return 0;
937 break;
938 default:
939 break;
940 }
941
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200942 /* Optionally patch the message */
943 gbprox_process_bssgp_dl(cfg, msg, peer);
944
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200945 return gbprox_relay2peer(msg, peer, ns_bvci);
946}
947
Harald Welte9f75c352010-04-30 20:26:32 +0200948/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +0200949static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
950 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200951 uint16_t ns_bvci)
952{
Harald Welteca3620a2010-05-03 16:30:59 +0200953 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200954 struct tlv_parsed tp;
955 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200956 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200957 struct gbproxy_peer *from_peer = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200958 struct gprs_ra_id raid;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200959 int copy_to_sgsn2 = 0;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200960 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200961
Harald Weltec471d3d2011-02-06 17:13:12 +0100962 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200963 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200964 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200965 return -EINVAL;
966 }
967
968 /* we actually should never see those two for BVCI == 0, but double-check
969 * just to make sure */
970 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
971 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200972 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200973 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200974 return -EINVAL;
975 }
976
977 bssgp_tlv_parse(&tp, bgph->data, data_len);
978
979 switch (pdu_type) {
980 case BSSGP_PDUT_SUSPEND:
981 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200982 /* We implement RAI snooping during SUSPEND/RESUME, since it
983 * establishes a relationsip between BVCI/peer and the routeing
984 * area identification. The snooped information is then used
985 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
986 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200987 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
988 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +0200989 from_peer = gbproxy_peer_by_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200990 if (!from_peer)
991 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200992 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
993 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200994 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200995 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200996 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200997 nsei, raid.mcc, raid.mnc, raid.lac,
998 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200999 /* FIXME: This only supports one BSS per RA */
1000 break;
Harald Welte44c48302010-05-03 19:22:32 +02001001 case BSSGP_PDUT_BVC_RESET:
1002 /* If we receive a BVC reset on the signalling endpoint, we
1003 * don't want the SGSN to reset, as the signalling endpoint
1004 * is common for all point-to-point BVCs (and thus all BTS) */
1005 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001006 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +02001007 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001008 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +02001009 if (bvci == 0) {
1010 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +02001011 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001012 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +02001013 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001014 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +02001015 }
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001016 from_peer = gbproxy_peer_by_bvci(cfg, bvci);
Harald Welte1174c082010-05-12 00:07:29 +02001017 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +02001018 /* if a PTP-BVC is reset, and we don't know that
1019 * PTP-BVCI yet, we should allocate a new peer */
1020 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001021 "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);
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001023 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +02001024 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +02001025
1026 if (!check_peer_nsei(from_peer, nsei))
1027 from_peer->nsei = nsei;
1028
Harald Welte1174c082010-05-12 00:07:29 +02001029 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
1030 struct gprs_ra_id raid;
1031 /* We have a Cell Identifier present in this
1032 * PDU, this means we can extend our local
1033 * state information about this particular cell
1034 * */
1035 memcpy(from_peer->ra,
1036 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
1037 sizeof(from_peer->ra));
1038 gsm48_parse_ra(&raid, from_peer->ra);
1039 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001040 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +02001041 bvci, raid.mcc, raid.mnc, raid.lac,
1042 raid.rac);
1043 }
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001044 if (cfg->route_to_sgsn2)
1045 copy_to_sgsn2 = 1;
Harald Welte44c48302010-05-03 19:22:32 +02001046 }
1047 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001048 }
1049
Harald Weltee9ea2692010-05-11 20:20:13 +02001050 /* Normally, we can simply pass on all signalling messages from BSS to
1051 * SGSN */
Jacob Erlbeck59300642014-09-08 09:04:01 +02001052 rc = gbprox_process_bssgp_ul(cfg, msg, from_peer);
1053 if (!rc)
1054 return 0;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001055
1056 if (copy_to_sgsn2)
1057 gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn2_nsei);
1058
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +02001059 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001060err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +02001061 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001062 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001063 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001064 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001065err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +02001066 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001067 nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001068 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001069 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001070}
1071
1072/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001073static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001074 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001075{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001076 struct gbproxy_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001077 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +02001078
Harald Welte4cf12e92010-05-13 14:14:56 +02001079 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001080 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +02001081 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001082 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +02001083 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
1084 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001085 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +02001086 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001087 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001088 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001089 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001090 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001091 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001092 peer = gbproxy_peer_by_lai(cfg, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001093 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +08001094 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001095 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +02001096 } else
Harald Welte4cf12e92010-05-13 14:14:56 +02001097 LOGPC(DGPRS, LOGL_INFO, "\n");
1098
1099 if (!peer) {
1100 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001101 "unable to route, missing IE\n", nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001102 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +02001103 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +02001104 }
1105 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001106}
1107
Harald Welte0a4050c2010-05-11 10:01:17 +02001108/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001109static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001110 struct msgb *orig_msg,
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001111 struct msgb *msg, struct tlv_parsed *tp,
1112 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +02001113{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001114 struct gbproxy_peer *peer;
Harald Welte0a4050c2010-05-11 10:01:17 +02001115 uint16_t ptp_bvci;
1116
1117 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001118 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001119 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001120 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001121 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001122 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001123 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +02001124
1125 if (ptp_bvci >= 2) {
1126 /* A reset for a PTP BVC was received, forward it to its
1127 * respective peer */
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001128 peer = gbproxy_peer_by_bvci(cfg, ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001129 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001130 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001131 nsei, ptp_bvci);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001132 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001133 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +02001134 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +02001135 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +02001136 }
1137 return gbprox_relay2peer(msg, peer, ns_bvci);
1138 }
1139
1140 /* A reset for the Signalling entity has been received
1141 * from the SGSN. As the signalling BVCI is shared
1142 * among all the BSS's that we multiplex, it needs to
1143 * be relayed */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001144 llist_for_each_entry(peer, &cfg->bts_peers, list)
Harald Welte0a4050c2010-05-11 10:01:17 +02001145 gbprox_relay2peer(msg, peer, ns_bvci);
1146
1147 return 0;
1148}
1149
Harald Welte9f75c352010-04-30 20:26:32 +02001150/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001151static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001152 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001153 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +02001154{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001155 struct bssgp_normal_hdr *bgph =
1156 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001157 struct tlv_parsed tp;
1158 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001159 int data_len;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001160 struct gbproxy_peer *peer;
Harald Welte9f75c352010-04-30 20:26:32 +02001161 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001162 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +02001163 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001164 int cause;
Harald Welte9f75c352010-04-30 20:26:32 +02001165
Harald Weltec471d3d2011-02-06 17:13:12 +01001166 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +02001167 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001168 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001169 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +02001170 return -EINVAL;
1171 }
1172
1173 /* we actually should never see those two for BVCI == 0, but double-check
1174 * just to make sure */
1175 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
1176 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +02001177 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001178 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001179 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001180 }
1181
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001182 msg = gprs_msgb_copy(orig_msg, "rx_sig_from_sgsn");
1183 gbprox_process_bssgp_dl(cfg, msg, NULL);
1184 /* Update message info */
1185 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1186 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +02001187 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1188
1189 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +02001190 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001191 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001192 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001193 case BSSGP_PDUT_BVC_RESET_ACK:
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001194 if (cfg->route_to_sgsn2 && nsei == cfg->nsip_sgsn2_nsei)
1195 break;
1196 /* fall through */
1197 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001198 /* simple case: BVCI IE is mandatory */
1199 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1200 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001201 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001202 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001203 break;
1204 case BSSGP_PDUT_PAGING_PS:
1205 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001206 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001207 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001208 break;
1209 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001210 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +02001211 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001212 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +02001213 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
1214 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1215 goto err_mand_ie;
1216 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001217 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +02001218 LOGPC(DGPRS, LOGL_NOTICE,
1219 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001220 bssgp_cause_str(cause));
Harald Welte0a4050c2010-05-11 10:01:17 +02001221 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001222 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1223 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%u\n", bvci);
1224
1225 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
1226 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001227 } else
1228 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001229 break;
1230 /* those only exist in the SGSN -> BSS direction */
1231 case BSSGP_PDUT_SUSPEND_ACK:
1232 case BSSGP_PDUT_SUSPEND_NACK:
1233 case BSSGP_PDUT_RESUME_ACK:
1234 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001235 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +02001236 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
1237 goto err_mand_ie;
Jacob Erlbeck5f1faa32014-08-21 10:01:30 +02001238 peer = gbproxy_peer_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +02001239 if (!peer)
1240 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +02001241 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001242 break;
Harald Welte36f98d92011-02-06 13:09:29 +01001243 case BSSGP_PDUT_BVC_BLOCK_ACK:
1244 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1245 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1246 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001247 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +01001248 if (bvci == 0) {
1249 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001250 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +01001251 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
1252 /* should we send STATUS ? */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001253 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001254 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +01001255 } else {
1256 /* Mark BVC as (un)blocked */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001257 block_unblock_peer(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +01001258 }
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001259 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +01001260 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001261 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +02001262 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001263 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001264 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001265 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +02001266 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001267 break;
1268 default:
Harald Welte72953b82010-05-12 00:20:41 +02001269 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
1270 pdu_type);
Holger Hans Peter Freythereece62772014-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 Freythereece62772014-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 Freythereece62772014-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 Freythereece62772014-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 Freythereece62772014-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 Freythereece62772014-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 Freythereece62772014-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 Freythereece62772014-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) {
1373 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
1374 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
1375 nsvc->nsvci);
1376 return 0;
1377 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001378 switch (signal) {
1379 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +02001380 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +01001381 if (!peer->blocked)
1382 break;
1383 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
1384 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
1385 nsvc->nsei, nsvc->nsvci);
1386 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
1387 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +02001388 break;
Harald Welte5e106d72011-02-06 16:33:29 +01001389 }
1390 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001391 /* Forward this message to all NS-VC to BSS */
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001392 struct gprs_ns_inst *nsi = cfg->nsi;
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001393 struct gprs_nsvc *next_nsvc;
1394
1395 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
1396 if (next_nsvc->remote_end_is_sgsn)
1397 continue;
1398
1399 /* Note that the following does not start the full
1400 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +01001401 switch (signal) {
1402 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001403 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001404 break;
1405 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001406 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001407 break;
1408 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +02001409 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +01001410 break;
1411 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001412 }
1413 }
1414 return 0;
1415}
1416
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001417void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001418{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +02001419 struct gbproxy_peer *peer, *tmp;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001420
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001421 llist_for_each_entry_safe(peer, tmp, &cfg->bts_peers, list)
Holger Hans Peter Freyther0196c992014-08-04 17:10:08 +02001422 gbproxy_peer_free(peer);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001423
Holger Hans Peter Freythereece62772014-08-04 15:42:36 +02001424 rate_ctr_group_free(cfg->ctrg);
1425 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001426}
1427
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001428int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001429{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001430 struct timespec tp;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001431
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001432 INIT_LLIST_HEAD(&cfg->bts_peers);
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +02001433 cfg->ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001434 clock_gettime(CLOCK_REALTIME, &tp);
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001435
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001436 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001437}