blob: 82e9e8c708ad6cad97872d7734f28ddd0bddf1d2 [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>
Harald Welte9f75c352010-04-30 20:26:32 +020032
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010033#include <osmocom/core/talloc.h>
34#include <osmocom/core/select.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020035#include <osmocom/core/rate_ctr.h>
36
37#include <osmocom/vty/misc.h>
Harald Welte9f75c352010-04-30 20:26:32 +020038
Harald Welteea34a4e2012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_ns.h>
40#include <osmocom/gprs/gprs_bssgp.h>
41
Harald Welte9f75c352010-04-30 20:26:32 +020042#include <openbsc/signal.h>
43#include <openbsc/debug.h>
Harald Welte672f5c42010-05-03 18:54:58 +020044#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020045
Jacob Erlbeck67a44452014-05-19 10:14:58 +020046#include <openbsc/gprs_llc.h>
47#include <openbsc/gsm_04_08.h>
48#include <openbsc/gsm_04_08_gprs.h>
49
Jacob Erlbeckbc555742013-10-18 14:34:55 +020050enum gbprox_global_ctr {
51 GBPROX_GLOB_CTR_INV_BVCI,
Jacob Erlbeck8f503592014-06-02 10:49:00 +020052 GBPROX_GLOB_CTR_INV_LAI,
53 GBPROX_GLOB_CTR_INV_RAI,
Jacob Erlbeckbc555742013-10-18 14:34:55 +020054 GBPROX_GLOB_CTR_INV_NSEI,
55 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
56 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
57 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
58 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
59 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
60 GBPROX_GLOB_CTR_TX_ERR_SGSN,
61 GBPROX_GLOB_CTR_OTHER_ERR,
Jacob Erlbeck67a44452014-05-19 10:14:58 +020062 GBPROX_GLOB_CTR_RAID_PATCHED_BSS,
63 GBPROX_GLOB_CTR_RAID_PATCHED_SGSN,
64 GBPROX_GLOB_CTR_PATCH_ERR,
Jacob Erlbeckbc555742013-10-18 14:34:55 +020065};
66
67static const struct rate_ctr_desc global_ctr_description[] = {
68 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020069 { "inv-lai", "Invalid Location Area Identifier" },
70 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020071 { "inv-nsei", "No BVC established for NSEI " },
72 { "proto-err.bss", "BSSGP protocol error (BSS )" },
73 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
74 { "not-supp.bss", "Feature not supported (BSS )" },
75 { "not-supp.sgsn", "Feature not supported (SGSN)" },
76 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
77 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
78 { "error", "Other error " },
Jacob Erlbeck67a44452014-05-19 10:14:58 +020079 { "raid-mod.bss", "RAID patched (BSS )" },
80 { "raid-mod.sgsn", "RAID patched (SGSN)" },
81 { "mod-err", "Patching error " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020082};
83
84static const struct rate_ctr_group_desc global_ctrg_desc = {
85 .group_name_prefix = "gbproxy.global",
86 .group_description = "GBProxy Global Statistics",
87 .num_ctr = ARRAY_SIZE(global_ctr_description),
88 .ctr_desc = global_ctr_description,
89};
90
91static struct rate_ctr_group *global_ctrg = NULL;
92
93static struct rate_ctr_group *get_global_ctrg()
94{
95 if (global_ctrg)
96 return global_ctrg;
97
98 global_ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
99 return global_ctrg;
100}
101
102enum gbprox_peer_ctr {
103 GBPROX_PEER_CTR_BLOCKED,
104 GBPROX_PEER_CTR_UNBLOCKED,
105 GBPROX_PEER_CTR_DROPPED,
106 GBPROX_PEER_CTR_INV_NSEI,
107 GBPROX_PEER_CTR_TX_ERR,
108};
109
110static const struct rate_ctr_desc peer_ctr_description[] = {
111 { "blocked", "BVC Block " },
112 { "unblocked", "BVC Unblock " },
113 { "dropped", "BVC blocked, dropped packet " },
114 { "inv-nsei", "NSEI mismatch " },
115 { "tx-err", "NS Transmission error " },
116};
117
118static const struct rate_ctr_group_desc peer_ctrg_desc = {
119 .group_name_prefix = "gbproxy.peer",
120 .group_description = "GBProxy Peer Statistics",
121 .num_ctr = ARRAY_SIZE(peer_ctr_description),
122 .ctr_desc = peer_ctr_description,
123};
124
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200125static struct gbprox_patch_state {
126 int local_mnc;
127 int local_mcc;
128} gbprox_patch_state = {0};
129
Harald Welte9f75c352010-04-30 20:26:32 +0200130struct gbprox_peer {
131 struct llist_head list;
132
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200133 /* NSEI of the peer entity */
134 uint16_t nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200135
136 /* BVCI used for Point-to-Point to this peer */
137 uint16_t bvci;
Harald Welte36f98d92011-02-06 13:09:29 +0100138 int blocked;
Harald Welte9f75c352010-04-30 20:26:32 +0200139
140 /* Routeing Area that this peer is part of (raw 04.08 encoding) */
141 uint8_t ra[6];
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200142
143 /* Counter */
144 struct rate_ctr_group *ctrg;
Harald Welte9f75c352010-04-30 20:26:32 +0200145};
146
147/* Linked list of all Gb peers (except SGSN) */
148static LLIST_HEAD(gbprox_bts_peers);
149
Harald Welte9f75c352010-04-30 20:26:32 +0200150/* Find the gbprox_peer by its BVCI */
151static struct gbprox_peer *peer_by_bvci(uint16_t bvci)
152{
153 struct gbprox_peer *peer;
154 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
155 if (peer->bvci == bvci)
156 return peer;
157 }
158 return NULL;
159}
160
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200161/* Find the gbprox_peer by its NSEI */
162static struct gbprox_peer *peer_by_nsei(uint16_t nsei)
Harald Welte9f75c352010-04-30 20:26:32 +0200163{
164 struct gbprox_peer *peer;
165 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200166 if (peer->nsei == nsei)
Harald Welte9f75c352010-04-30 20:26:32 +0200167 return peer;
168 }
169 return NULL;
170}
171
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200172/* look-up a peer by its Routeing Area Identification (RAI) */
173static struct gbprox_peer *peer_by_rai(const uint8_t *ra)
Harald Welte9f75c352010-04-30 20:26:32 +0200174{
175 struct gbprox_peer *peer;
176 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Harald Welte1174c082010-05-12 00:07:29 +0200177 if (!memcmp(peer->ra, ra, 6))
Harald Welte9f75c352010-04-30 20:26:32 +0200178 return peer;
179 }
180 return NULL;
181}
182
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200183/* look-up a peer by its Location Area Identification (LAI) */
184static struct gbprox_peer *peer_by_lai(const uint8_t *la)
Harald Welte9f75c352010-04-30 20:26:32 +0200185{
186 struct gbprox_peer *peer;
187 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Harald Welte1174c082010-05-12 00:07:29 +0200188 if (!memcmp(peer->ra, la, 5))
Harald Welte9f75c352010-04-30 20:26:32 +0200189 return peer;
190 }
191 return NULL;
192}
193
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200194static int check_peer_nsei(struct gbprox_peer *peer, uint16_t nsei)
195{
196 if (peer->nsei != nsei) {
197 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
198 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
199 peer->bvci, nsei, peer->nsei);
200 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200201 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200202 }
203
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200204 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200205}
206
Harald Welte9f75c352010-04-30 20:26:32 +0200207static struct gbprox_peer *peer_alloc(uint16_t bvci)
208{
209 struct gbprox_peer *peer;
210
211 peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer);
212 if (!peer)
213 return NULL;
214
215 peer->bvci = bvci;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200216 peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci);
217
Harald Welte9f75c352010-04-30 20:26:32 +0200218 llist_add(&peer->list, &gbprox_bts_peers);
219
220 return peer;
221}
222
223static void peer_free(struct gbprox_peer *peer)
224{
Jacob Erlbeck02ca7782013-10-23 11:24:16 +0200225 rate_ctr_group_free(peer->ctrg);
Harald Welte9f75c352010-04-30 20:26:32 +0200226 llist_del(&peer->list);
227 talloc_free(peer);
228}
229
Harald Welte39d0bb52010-05-12 18:10:25 +0000230/* FIXME: this needs to go to libosmocore/msgb.c */
231static struct msgb *msgb_copy(const struct msgb *msg, const char *name)
232{
Harald Welte8645e102012-06-16 16:09:52 +0800233 struct libgb_msgb_cb *old_cb, *new_cb;
Harald Welte39d0bb52010-05-12 18:10:25 +0000234 struct msgb *new_msg;
235
Holger Hans Peter Freyther62d97982010-06-08 18:14:37 +0800236 new_msg = msgb_alloc(msg->data_len, name);
Harald Welte39d0bb52010-05-12 18:10:25 +0000237 if (!new_msg)
238 return NULL;
239
Harald Welte39d0bb52010-05-12 18:10:25 +0000240 /* copy data */
Holger Hans Peter Freyther62d97982010-06-08 18:14:37 +0800241 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
242
243 /* copy header */
244 new_msg->len = msg->len;
245 new_msg->data += msg->data - msg->_data;
246 new_msg->head += msg->head - msg->_data;
247 new_msg->tail += msg->tail - msg->_data;
248
249 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
250 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
251 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
252 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
253
254 /* copy GB specific data */
Harald Welte8645e102012-06-16 16:09:52 +0800255 old_cb = LIBGB_MSGB_CB(msg);
256 new_cb = LIBGB_MSGB_CB(new_msg);
Holger Hans Peter Freyther62d97982010-06-08 18:14:37 +0800257
258 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
259 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
260
Harald Weltefb9e06f2011-02-06 17:17:05 +0100261 /* bssgp_cell_id is a pointer into the old msgb, so we need to make
262 * it a pointer into the new msgb */
263 new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data);
Holger Hans Peter Freyther62d97982010-06-08 18:14:37 +0800264 new_cb->nsei = old_cb->nsei;
265 new_cb->bvci = old_cb->bvci;
266 new_cb->tlli = old_cb->tlli;
Harald Welte39d0bb52010-05-12 18:10:25 +0000267
268 return new_msg;
269}
270
Harald Welte69619e32010-05-03 19:05:10 +0200271/* strip off the NS header */
272static void strip_ns_hdr(struct msgb *msg)
273{
274 int strip_len = msgb_bssgph(msg) - msg->data;
275 msgb_pull(msg, strip_len);
276}
277
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200278/* patch RA identifier in place, update peer accordingly */
279static void gbprox_patch_raid(uint8_t *raid_enc, struct gbprox_patch_state *state,
280 int to_bss, const char *log_text)
281{
282 const int old_local_mcc = state->local_mcc;
283 const int old_local_mnc = state->local_mnc;
284 int old_mcc;
285 int old_mnc;
286 struct gprs_ra_id raid;
287
288 gsm48_parse_ra(&raid, raid_enc);
289
290 old_mcc = raid.mcc;
291 old_mnc = raid.mnc;
292
293 if (!to_bss) {
294 /* BSS -> SGSN */
295 /* save BSS side MCC/MNC */
296 if (!gbcfg.core_mcc || raid.mcc == gbcfg.core_mcc) {
297 state->local_mcc = 0;
298 } else {
299 state->local_mcc = raid.mcc;
300 raid.mcc = gbcfg.core_mcc;
301 }
302
303 if (!gbcfg.core_mnc || raid.mnc == gbcfg.core_mnc) {
304 state->local_mnc = 0;
305 } else {
306 state->local_mnc = raid.mnc;
307 raid.mnc = gbcfg.core_mnc;
308 }
309 } else {
310 /* SGSN -> BSS */
311 if (state->local_mcc)
312 raid.mcc = state->local_mcc;
313
314 if (state->local_mnc)
315 raid.mnc = state->local_mnc;
316 }
317
318 if (old_local_mcc != state->local_mcc ||
319 old_local_mnc != state->local_mnc)
320 LOGP(DGPRS, LOGL_NOTICE,
321 "Patching RAID %sactivated, msg: %s, "
322 "local: %d-%d, core: %d-%d, to %s\n",
323 state->local_mcc || state->local_mnc ?
324 "" : "de",
325 log_text,
326 state->local_mcc, state->local_mnc,
327 gbcfg.core_mcc, gbcfg.core_mnc,
328 to_bss ? "BSS" : "SGSN");
329
330 if (state->local_mcc || state->local_mnc) {
331 enum gbprox_global_ctr counter =
332 to_bss ?
333 GBPROX_GLOB_CTR_RAID_PATCHED_SGSN :
334 GBPROX_GLOB_CTR_RAID_PATCHED_BSS;
335
336 LOGP(DGPRS, LOGL_DEBUG,
337 "Patching %s to %s: "
338 "%d-%d-%d-%d -> %d-%d-%d-%d\n",
339 log_text,
340 to_bss ? "BSS" : "SGSN",
341 old_mcc, old_mnc, raid.lac, raid.rac,
342 raid.mcc, raid.mnc, raid.lac, raid.rac);
343
344 gsm48_construct_ra(raid_enc, &raid);
345 rate_ctr_inc(&get_global_ctrg()->ctr[counter]);
346 }
347}
348
349/* patch BSSGP message to use core_mcc/mnc on the SGSN side */
350static void gbprox_patch_bssgp_message(struct msgb *msg, int to_bss)
351{
352 struct bssgp_normal_hdr *bgph;
353 struct bssgp_ud_hdr *budh;
354 struct tlv_parsed tp;
355 uint8_t pdu_type;
356 struct gbprox_patch_state *state = &gbprox_patch_state;
357 uint8_t *data;
358 size_t data_len;
359
360 if (!gbcfg.core_mcc && !gbcfg.core_mnc)
361 return;
362
363 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
364 budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
365 pdu_type = bgph->pdu_type;
366
367 if (to_bss && !state->local_mcc && !state->local_mnc)
368 return;
369
370 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
371 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
372 data = budh->data;
373 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
374 } else {
375 data = bgph->data;
376 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
377 }
378
379 bssgp_tlv_parse(&tp, data, data_len);
380
381 if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
382 gbprox_patch_raid((uint8_t *)TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
383 state, to_bss, "ROUTING_AREA");
384
385 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID))
386 gbprox_patch_raid((uint8_t *)TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
387 state, to_bss, "CELL_ID");
388}
389
Harald Welte9f75c352010-04-30 20:26:32 +0200390/* feed a message down the NS-VC associated with the specified peer */
Harald Welte39d0bb52010-05-12 18:10:25 +0000391static int gbprox_relay2sgsn(struct msgb *old_msg, uint16_t ns_bvci)
Harald Welte672f5c42010-05-03 18:54:58 +0200392{
Harald Welte39d0bb52010-05-12 18:10:25 +0000393 /* create a copy of the message so the old one can
394 * be free()d safely when we return from gbprox_rcvmsg() */
395 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200396 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000397
Harald Weltee9ea2692010-05-11 20:20:13 +0200398 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Harald Welte96f71f22010-05-03 19:28:05 +0200399 msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200400
Harald Welte672f5c42010-05-03 18:54:58 +0200401 msgb_bvci(msg) = ns_bvci;
402 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
403
Harald Welte69619e32010-05-03 19:05:10 +0200404 strip_ns_hdr(msg);
405
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200406 gbprox_patch_bssgp_message(msg, 0);
407
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200408 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
409 if (rc < 0)
410 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
411
412 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200413}
414
Harald Welte672f5c42010-05-03 18:54:58 +0200415/* feed a message down the NS-VC associated with the specified peer */
Harald Welte39d0bb52010-05-12 18:10:25 +0000416static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200417 uint16_t ns_bvci)
418{
Harald Welte39d0bb52010-05-12 18:10:25 +0000419 /* create a copy of the message so the old one can
420 * be free()d safely when we return from gbprox_rcvmsg() */
421 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200422 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000423
Harald Welte0ab535b2010-05-13 10:34:56 +0200424 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200425 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200426
Harald Welte9f75c352010-04-30 20:26:32 +0200427 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200428 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200429
Harald Welte0ab535b2010-05-13 10:34:56 +0200430 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200431 strip_ns_hdr(msg);
432
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200433 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
434 if (rc < 0)
435 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
436
437 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200438}
439
Harald Welte36f98d92011-02-06 13:09:29 +0100440static int block_unblock_peer(uint16_t ptp_bvci, uint8_t pdu_type)
441{
442 struct gbprox_peer *peer;
443
444 peer = peer_by_bvci(ptp_bvci);
445 if (!peer) {
446 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
447 ptp_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200448 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100449 return -ENOENT;
450 }
451
452 switch (pdu_type) {
453 case BSSGP_PDUT_BVC_BLOCK_ACK:
454 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200455 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100456 break;
457 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
458 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200459 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100460 break;
461 default:
462 break;
463 }
464 return 0;
465}
466
Harald Welte9f75c352010-04-30 20:26:32 +0200467/* Send a message to a peer identified by ptp_bvci but using ns_bvci
468 * in the NS hdr */
Harald Welte69619e32010-05-03 19:05:10 +0200469static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200470 uint16_t ns_bvci)
471{
472 struct gbprox_peer *peer;
473
474 peer = peer_by_bvci(ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200475 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200476 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200477 ptp_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200478 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200479 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200480 }
Harald Welte9f75c352010-04-30 20:26:32 +0200481
Harald Welte69619e32010-05-03 19:05:10 +0200482 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200483}
484
Harald Welteb1fd9022012-06-17 12:16:31 +0800485int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
486{
487 return 0;
488}
489
Harald Welte9f75c352010-04-30 20:26:32 +0200490/* Receive an incoming signalling message from a BSS-side NS-VC */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200491static int gbprox_rx_sig_from_bss(struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200492 uint16_t ns_bvci)
493{
Harald Welteca3620a2010-05-03 16:30:59 +0200494 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200495 struct tlv_parsed tp;
496 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200497 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200498 struct gbprox_peer *from_peer;
Harald Welte70f38d22010-05-01 12:10:57 +0200499 struct gprs_ra_id raid;
Harald Welte9f75c352010-04-30 20:26:32 +0200500
Harald Weltec471d3d2011-02-06 17:13:12 +0100501 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200502 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200503 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200504 return -EINVAL;
505 }
506
507 /* we actually should never see those two for BVCI == 0, but double-check
508 * just to make sure */
509 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
510 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200511 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200512 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200513 return -EINVAL;
514 }
515
516 bssgp_tlv_parse(&tp, bgph->data, data_len);
517
518 switch (pdu_type) {
519 case BSSGP_PDUT_SUSPEND:
520 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200521 /* We implement RAI snooping during SUSPEND/RESUME, since it
522 * establishes a relationsip between BVCI/peer and the routeing
523 * area identification. The snooped information is then used
524 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
525 * BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200526 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
527 goto err_mand_ie;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200528 from_peer = peer_by_nsei(nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200529 if (!from_peer)
530 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200531 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
532 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200533 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200534 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200535 "RAI snooping: RAI %u-%u-%u-%u behind BVCI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200536 nsei, raid.mcc, raid.mnc, raid.lac,
537 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200538 /* FIXME: This only supports one BSS per RA */
539 break;
Harald Welte44c48302010-05-03 19:22:32 +0200540 case BSSGP_PDUT_BVC_RESET:
541 /* If we receive a BVC reset on the signalling endpoint, we
542 * don't want the SGSN to reset, as the signalling endpoint
543 * is common for all point-to-point BVCs (and thus all BTS) */
544 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200545 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +0200546 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200547 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +0200548 if (bvci == 0) {
549 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +0200550 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200551 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200552 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200553 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200554 }
555 from_peer = peer_by_bvci(bvci);
556 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +0200557 /* if a PTP-BVC is reset, and we don't know that
558 * PTP-BVCI yet, we should allocate a new peer */
559 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200560 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200561 from_peer = peer_alloc(bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200562 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +0200563 }
Jacob Erlbeck64cb9242013-10-24 12:48:56 +0200564
565 if (!check_peer_nsei(from_peer, nsei))
566 from_peer->nsei = nsei;
567
Harald Welte1174c082010-05-12 00:07:29 +0200568 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
569 struct gprs_ra_id raid;
570 /* We have a Cell Identifier present in this
571 * PDU, this means we can extend our local
572 * state information about this particular cell
573 * */
574 memcpy(from_peer->ra,
575 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
576 sizeof(from_peer->ra));
577 gsm48_parse_ra(&raid, from_peer->ra);
578 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200579 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +0200580 bvci, raid.mcc, raid.mnc, raid.lac,
581 raid.rac);
582 }
Harald Welte44c48302010-05-03 19:22:32 +0200583 }
584 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200585 }
586
Harald Weltee9ea2692010-05-11 20:20:13 +0200587 /* Normally, we can simply pass on all signalling messages from BSS to
588 * SGSN */
Harald Welte69619e32010-05-03 19:05:10 +0200589 return gbprox_relay2sgsn(msg, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200590err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +0200591 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200592 nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200593 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200594 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200595err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200596 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200597 nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200598 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200599 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200600}
601
602/* Receive paging request from SGSN, we need to relay to proper BSS */
603static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200604 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200605{
Harald Welte4cf12e92010-05-13 14:14:56 +0200606 struct gbprox_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200607 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +0200608
Harald Welte4cf12e92010-05-13 14:14:56 +0200609 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200610 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200611 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200612 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +0200613 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
614 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200615 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +0200616 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200617 peer = peer_by_rai(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
618 LOGPC(DGPRS, LOGL_INFO, "routing by RAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800619 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200620 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200621 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200622 peer = peer_by_lai(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
623 LOGPC(DGPRS, LOGL_INFO, "routing by LAI to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800624 peer ? peer->bvci : -1);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200625 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte9f75c352010-04-30 20:26:32 +0200626 } else
Harald Welte4cf12e92010-05-13 14:14:56 +0200627 LOGPC(DGPRS, LOGL_INFO, "\n");
628
629 if (!peer) {
630 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200631 "unable to route, missing IE\n", nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200632 rate_ctr_inc(&get_global_ctrg()->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +0200633 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +0200634 }
635 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200636}
637
Harald Welte0a4050c2010-05-11 10:01:17 +0200638/* Receive an incoming BVC-RESET message from the SGSN */
639static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200640 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +0200641{
642 struct gbprox_peer *peer;
643 uint16_t ptp_bvci;
644
645 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200646 rate_ctr_inc(&get_global_ctrg()->
647 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200648 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
649 NULL, msg);
650 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200651 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +0200652
653 if (ptp_bvci >= 2) {
654 /* A reset for a PTP BVC was received, forward it to its
655 * respective peer */
656 peer = peer_by_bvci(ptp_bvci);
657 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200658 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200659 nsei, ptp_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200660 rate_ctr_inc(&get_global_ctrg()->
661 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200662 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
663 NULL, msg);
664 }
665 return gbprox_relay2peer(msg, peer, ns_bvci);
666 }
667
668 /* A reset for the Signalling entity has been received
669 * from the SGSN. As the signalling BVCI is shared
670 * among all the BSS's that we multiplex, it needs to
671 * be relayed */
672 llist_for_each_entry(peer, &gbprox_bts_peers, list)
673 gbprox_relay2peer(msg, peer, ns_bvci);
674
675 return 0;
676}
677
Harald Welte9f75c352010-04-30 20:26:32 +0200678/* Receive an incoming signalling message from the SGSN-side NS-VC */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200679static int gbprox_rx_sig_from_sgsn(struct msgb *msg, uint32_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200680 uint16_t ns_bvci)
681{
Harald Welteca3620a2010-05-03 16:30:59 +0200682 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200683 struct tlv_parsed tp;
684 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200685 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200686 struct gbprox_peer *peer;
687 uint16_t bvci;
688 int rc = 0;
689
Harald Weltec471d3d2011-02-06 17:13:12 +0100690 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200691 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200692 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200693 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +0200694 return -EINVAL;
695 }
696
697 /* we actually should never see those two for BVCI == 0, but double-check
698 * just to make sure */
699 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
700 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200701 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200702 "signalling\n", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200703 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200704 }
705
706 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
707
708 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +0200709 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200710 rc = rx_reset_from_sgsn(msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200711 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200712 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +0200713 case BSSGP_PDUT_BVC_RESET_ACK:
714 /* simple case: BVCI IE is mandatory */
715 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
716 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200717 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte69619e32010-05-03 19:05:10 +0200718 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200719 break;
720 case BSSGP_PDUT_PAGING_PS:
721 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200722 /* process the paging request (LAI/RAI lookup) */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200723 rc = gbprox_rx_paging(msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200724 break;
725 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +0200726 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +0200727 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200728 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200729 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
730 LOGPC(DGPRS, LOGL_NOTICE, "\n");
731 goto err_mand_ie;
732 }
733 LOGPC(DGPRS, LOGL_NOTICE,
734 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
735 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
736 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200737 uint16_t bvci = tlvp_val16_unal(&tp, BSSGP_IE_BVCI);
Harald Welte0a4050c2010-05-11 10:01:17 +0200738 LOGPC(DGPRS, LOGL_NOTICE,
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200739 "BVCI=%u\n", ntohs(bvci));
Harald Welte0a4050c2010-05-11 10:01:17 +0200740 } else
741 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +0200742 break;
743 /* those only exist in the SGSN -> BSS direction */
744 case BSSGP_PDUT_SUSPEND_ACK:
745 case BSSGP_PDUT_SUSPEND_NACK:
746 case BSSGP_PDUT_RESUME_ACK:
747 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200748 /* RAI IE is mandatory */
Harald Welte9f75c352010-04-30 20:26:32 +0200749 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
750 goto err_mand_ie;
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200751 peer = peer_by_rai(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte9f75c352010-04-30 20:26:32 +0200752 if (!peer)
753 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +0200754 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200755 break;
Harald Welte36f98d92011-02-06 13:09:29 +0100756 case BSSGP_PDUT_BVC_BLOCK_ACK:
757 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
758 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
759 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200760 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +0100761 if (bvci == 0) {
762 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200763 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +0100764 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
765 /* should we send STATUS ? */
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200766 rate_ctr_inc(&get_global_ctrg()->
767 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100768 } else {
769 /* Mark BVC as (un)blocked */
770 block_unblock_peer(bvci, pdu_type);
771 }
772 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
773 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200774 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +0200775 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200776 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200777 rate_ctr_inc(&get_global_ctrg()->
778 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200779 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200780 break;
781 default:
Harald Welte72953b82010-05-12 00:20:41 +0200782 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
783 pdu_type);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200784 rate_ctr_inc(&get_global_ctrg()->
785 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200786 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200787 break;
788 }
789
790 return rc;
791err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +0200792 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200793 nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200794 rate_ctr_inc(&get_global_ctrg()->
795 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200796 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200797err_no_peer:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200798 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200799 nsei);
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200800 rate_ctr_inc(&get_global_ctrg()-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200801 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200802}
803
804/* Main input function for Gb proxy */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200805int gbprox_rcvmsg(struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200806{
Harald Welte672f5c42010-05-03 18:54:58 +0200807 int rc;
Harald Welte36f98d92011-02-06 13:09:29 +0100808 struct gbprox_peer *peer;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200809 int remote_end_is_sgsn = nsei == gbcfg.nsip_sgsn_nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200810
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200811 if (remote_end_is_sgsn)
812 gbprox_patch_bssgp_message(msg, 1);
813
Harald Welte9f75c352010-04-30 20:26:32 +0200814 /* Only BVCI=0 messages need special treatment */
815 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200816 if (remote_end_is_sgsn)
817 rc = gbprox_rx_sig_from_sgsn(msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200818 else
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200819 rc = gbprox_rx_sig_from_bss(msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +0200820 } else {
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200821 peer = peer_by_bvci(ns_bvci);
822
Harald Welte672f5c42010-05-03 18:54:58 +0200823 /* All other BVCI are PTP and thus can be simply forwarded */
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200824 if (!remote_end_is_sgsn) {
825 if (peer)
826 check_peer_nsei(peer, nsei);
Harald Welte36f98d92011-02-06 13:09:29 +0100827 return gbprox_relay2sgsn(msg, ns_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200828 }
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200829
Harald Welte36f98d92011-02-06 13:09:29 +0100830 /* else: SGSN -> BSS direction */
Harald Welte36f98d92011-02-06 13:09:29 +0100831 if (!peer) {
Jacob Erlbeckf0f63a42013-10-18 22:12:17 +0200832 LOGP(DGPRS, LOGL_INFO, "Didn't find peer for "
833 "BVCI=%u for message from NSVC=%u/NSEI=%u (SGSN)\n",
834 ns_bvci, nsvci, nsei);
835 rate_ctr_inc(&get_global_ctrg()->
836 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
837 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
838 &ns_bvci, msg);
Harald Welte36f98d92011-02-06 13:09:29 +0100839 }
840 if (peer->blocked) {
841 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
842 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200843 ns_bvci, nsvci, nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200844 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100845 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, NULL, msg);
846 }
847 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200848 }
849
Harald Welte672f5c42010-05-03 18:54:58 +0200850 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200851}
Harald Welte85801d02010-05-11 05:49:43 +0200852
Harald Welte1ccbf442010-05-14 11:53:08 +0000853int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
854{
855 struct gprs_nsvc *nsvc;
856
857 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
858 if (!nsvc->persistent)
859 continue;
860 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
861 }
862 return 0;
863}
864
Harald Weltec1c1dd22010-05-11 06:34:24 +0200865/* Signal handler for signals from NS layer */
866int gbprox_signal(unsigned int subsys, unsigned int signal,
867 void *handler_data, void *signal_data)
868{
869 struct ns_signal_data *nssd = signal_data;
870 struct gprs_nsvc *nsvc = nssd->nsvc;
871 struct gbprox_peer *peer;
872
Harald Weltea6a20b42012-06-16 16:40:42 +0800873 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +0200874 return 0;
875
Harald Weltef69c0592010-05-11 18:29:44 +0200876 if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) {
877 /* We have received a NS-RESET from the NSEI and NSVC
878 * of the SGSN. This might happen with SGSN that start
879 * their own NS-RESET procedure without waiting for our
880 * NS-RESET */
881 nsvc->remote_end_is_sgsn = 1;
882 }
883
Harald Welteb778d2c2010-05-12 13:28:25 +0000884 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
885 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
886 "re-starting RESET procedure\n");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200887 rate_ctr_inc(&get_global_ctrg()->
888 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +0800889 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
890 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +0000891 }
892
Harald Welte5e106d72011-02-06 16:33:29 +0100893 if (!nsvc->remote_end_is_sgsn) {
894 /* from BSS to SGSN */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200895 peer = peer_by_nsei(nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +0100896 if (!peer) {
897 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
898 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
899 nsvc->nsvci);
900 return 0;
901 }
Harald Weltec1c1dd22010-05-11 06:34:24 +0200902 switch (signal) {
903 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +0200904 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +0100905 if (!peer->blocked)
906 break;
907 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
908 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
909 nsvc->nsei, nsvc->nsvci);
910 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
911 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +0200912 break;
Harald Welte5e106d72011-02-06 16:33:29 +0100913 }
914 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200915 /* Forward this message to all NS-VC to BSS */
916 struct gprs_ns_inst *nsi = gbcfg.nsi;
917 struct gprs_nsvc *next_nsvc;
918
919 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
920 if (next_nsvc->remote_end_is_sgsn)
921 continue;
922
923 /* Note that the following does not start the full
924 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +0100925 switch (signal) {
926 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200927 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +0100928 break;
929 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200930 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +0100931 break;
932 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200933 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +0100934 break;
935 }
Harald Weltec1c1dd22010-05-11 06:34:24 +0200936 }
937 }
938 return 0;
939}
940
Jacob Erlbeckb32d3c02014-07-07 10:45:59 +0200941int gbprox_dump_global(FILE *stream, int indent)
Jacob Erlbeckda890c72013-10-18 22:12:16 +0200942{
943 unsigned int i;
944 const struct rate_ctr_group_desc *desc;
945 int rc;
946
947 rc = fprintf(stream, "%*sGbproxy global:\n", indent, "");
948 if (rc < 0)
949 return rc;
950
Jacob Erlbeckda890c72013-10-18 22:12:16 +0200951 desc = get_global_ctrg()->desc;
952
953 for (i = 0; i < desc->num_ctr; i++) {
954 struct rate_ctr *ctr = &get_global_ctrg()->ctr[i];
955 if (ctr->current) {
956 rc = fprintf(stream, "%*s %s: %llu\n",
957 indent, "",
958 desc->ctr_desc[i].description,
959 (long long)ctr->current);
960
961 if (rc < 0)
962 return rc;
963 }
964 }
965
966 return 0;
967}
968
Jacob Erlbeckb32d3c02014-07-07 10:45:59 +0200969int gbprox_dump_peers(FILE *stream, int indent)
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200970{
971 struct gbprox_peer *peer;
972 struct gprs_ra_id raid;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200973 unsigned int i;
974 const struct rate_ctr_group_desc *desc;
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200975 int rc;
976
Jacob Erlbeckda890c72013-10-18 22:12:16 +0200977 rc = fprintf(stream, "%*sPeers:\n", indent, "");
978 if (rc < 0)
979 return rc;
980
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200981 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
982 gsm48_parse_ra(&raid, peer->ra);
983
984 rc = fprintf(stream, "%*s NSEI %u, BVCI %u, %sblocked, "
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200985 "RAI %u-%u-%u-%u\n",
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200986 indent, "",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200987 peer->nsei, peer->bvci,
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200988 peer->blocked ? "" : "not ",
989 raid.mcc, raid.mnc, raid.lac, raid.rac);
990
991 if (rc < 0)
992 return rc;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200993
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200994 desc = peer->ctrg->desc;
995
996 for (i = 0; i < desc->num_ctr; i++) {
997 struct rate_ctr *ctr = &peer->ctrg->ctr[i];
998 if (ctr->current) {
999 rc = fprintf(stream, "%*s %s: %llu\n",
1000 indent, "",
1001 desc->ctr_desc[i].description,
1002 (long long)ctr->current);
1003
1004 if (rc < 0)
1005 return rc;
1006 }
1007 }
Jacob Erlbeck51a869c2013-10-15 12:00:26 +02001008 }
1009
1010 return 0;
1011}
Harald Welte85801d02010-05-11 05:49:43 +02001012
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001013void gbprox_reset()
1014{
1015 struct gbprox_peer *peer, *tmp;
1016
1017 llist_for_each_entry_safe(peer, tmp, &gbprox_bts_peers, list)
1018 peer_free(peer);
1019
1020 rate_ctr_group_free(global_ctrg);
1021 global_ctrg = NULL;
1022}
1023
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001024static int gbprox_cleanup_peers(uint16_t nsei, uint16_t bvci)
1025{
1026 int counter = 0;
1027 struct gbprox_peer *peer, *tmp;
1028
1029 llist_for_each_entry_safe(peer, tmp, &gbprox_bts_peers, list) {
1030 if (peer->nsei != nsei)
1031 continue;
1032 if (bvci && peer->bvci != bvci)
1033 continue;
1034
1035 peer_free(peer);
1036 counter += 1;
1037 }
1038
1039 return counter;
1040}
1041
Harald Welte4b037e42010-05-19 19:45:32 +02001042#include <osmocom/vty/command.h>
Harald Welte85801d02010-05-11 05:49:43 +02001043
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001044static void gbprox_vty_print_peer(struct vty *vty, struct gbprox_peer *peer)
1045{
1046 struct gprs_ra_id raid;
1047 gsm48_parse_ra(&raid, peer->ra);
1048
1049 vty_out(vty, "NSEI %5u, PTP-BVCI %5u, "
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001050 "RAI %u-%u-%u-%u",
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001051 peer->nsei, peer->bvci,
1052 raid.mcc, raid.mnc, raid.lac, raid.rac);
1053 if (peer->blocked)
1054 vty_out(vty, " [BVC-BLOCKED]");
1055
1056 vty_out(vty, "%s", VTY_NEWLINE);
1057}
1058
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001059gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy [stats]",
Holger Hans Peter Freyther82e644b2013-11-03 17:34:17 +01001060 SHOW_STR "Display information about the Gb proxy\n" "Show statistics\n")
Harald Welte85801d02010-05-11 05:49:43 +02001061{
1062 struct gbprox_peer *peer;
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001063 int show_stats = argc >= 1;
1064
1065 if (show_stats)
1066 vty_out_rate_ctr_group(vty, "", get_global_ctrg());
Harald Welte85801d02010-05-11 05:49:43 +02001067
1068 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001069 gbprox_vty_print_peer(vty, peer);
Jacob Erlbeckbc555742013-10-18 14:34:55 +02001070
1071 if (show_stats)
1072 vty_out_rate_ctr_group(vty, " ", peer->ctrg);
Harald Welte85801d02010-05-11 05:49:43 +02001073 }
1074 return CMD_SUCCESS;
1075}
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02001076
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001077gDEFUN(delete_gb_bvci, delete_gb_bvci_cmd,
1078 "delete-gbproxy-peer <0-65534> bvci <2-65534>",
1079 "Delete a GBProxy peer by NSEI and optionally BVCI\n"
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02001080 "NSEI number\n"
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001081 "Only delete peer with a matching BVCI\n"
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02001082 "BVCI number\n")
1083{
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02001084 const uint16_t nsei = atoi(argv[0]);
1085 const uint16_t bvci = atoi(argv[1]);
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001086 int counter;
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02001087
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001088 counter = gbprox_cleanup_peers(nsei, bvci);
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02001089
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001090 if (counter == 0) {
1091 vty_out(vty, "BVC not found%s", VTY_NEWLINE);
1092 return CMD_WARNING;
1093 }
1094
1095 return CMD_SUCCESS;
1096}
1097
1098gDEFUN(delete_gb_nsei, delete_gb_nsei_cmd,
1099 "delete-gbproxy-peer <0-65534> (only-bvc|only-nsvc|all) [dry-run]",
1100 "Delete a GBProxy peer by NSEI and optionally BVCI\n"
1101 "NSEI number\n"
1102 "Only delete BSSGP connections (BVC)\n"
1103 "Only delete dynamic NS connections (NS-VC)\n"
1104 "Delete BVC and dynamic NS connections\n"
1105 "Show what would be deleted instead of actually deleting\n"
1106 )
1107{
1108 const uint16_t nsei = atoi(argv[0]);
1109 const char *mode = argv[1];
1110 int dry_run = argc > 2;
1111 int delete_bvc = 0;
1112 int delete_nsvc = 0;
1113 int counter;
1114
1115 if (strcmp(mode, "only-bvc") == 0)
1116 delete_bvc = 1;
1117 else if (strcmp(mode, "only-nsvc") == 0)
1118 delete_nsvc = 1;
1119 else
1120 delete_bvc = delete_nsvc = 1;
1121
1122 if (delete_bvc) {
1123 if (!dry_run)
1124 counter = gbprox_cleanup_peers(nsei, 0);
1125 else {
1126 struct gbprox_peer *peer;
1127 counter = 0;
1128 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
1129 if (peer->nsei != nsei)
1130 continue;
1131
1132 vty_out(vty, "BVC: ");
1133 gbprox_vty_print_peer(vty, peer);
1134 counter += 1;
1135 }
1136 }
1137 vty_out(vty, "%sDeleted %d BVC%s",
1138 dry_run ? "Not " : "", counter, VTY_NEWLINE);
1139 }
1140
1141 if (delete_nsvc) {
1142 struct gprs_ns_inst *nsi = gbcfg.nsi;
1143 struct gprs_nsvc *nsvc, *nsvc2;
1144
1145 counter = 0;
1146 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list) {
1147 if (nsvc->nsei != nsei)
1148 continue;
1149 if (nsvc->persistent)
1150 continue;
1151
1152 if (!dry_run)
1153 gprs_nsvc_delete(nsvc);
1154 else
1155 vty_out(vty, "NS-VC: NSEI %5u, NS-VCI %5u, "
1156 "remote %s%s",
1157 nsvc->nsei, nsvc->nsvci,
1158 gprs_ns_ll_str(nsvc), VTY_NEWLINE);
1159 counter += 1;
1160 }
1161 vty_out(vty, "%sDeleted %d NS-VC%s",
1162 dry_run ? "Not " : "", counter, VTY_NEWLINE);
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02001163 }
1164
1165 return CMD_SUCCESS;
1166}