blob: ddc9e0aaf0b39a634c2bef34537eddc8405e2645 [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 Freyther85531cc2010-10-06 20:37:09 +08004 * (C) 2010 by On-Waves
Harald Welte9f75c352010-04-30 20:26:32 +02005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9f75c352010-04-30 20:26:32 +020010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9f75c352010-04-30 20:26:32 +020016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9f75c352010-04-30 20:26:32 +020019 *
20 */
21
22#include <unistd.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <getopt.h>
27#include <errno.h>
28#include <sys/fcntl.h>
29#include <sys/stat.h>
Harald Welte7fc98222010-05-11 10:15:42 +020030#include <arpa/inet.h>
Harald Welte9f75c352010-04-30 20:26:32 +020031
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/talloc.h>
33#include <osmocom/core/select.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020034#include <osmocom/core/rate_ctr.h>
35
36#include <osmocom/vty/misc.h>
Harald Welte9f75c352010-04-30 20:26:32 +020037
Harald Welteea34a4e2012-06-16 14:59:56 +080038#include <osmocom/gprs/gprs_ns.h>
39#include <osmocom/gprs/gprs_bssgp.h>
40
Harald Welte9f75c352010-04-30 20:26:32 +020041#include <openbsc/signal.h>
42#include <openbsc/debug.h>
Harald Welte672f5c42010-05-03 18:54:58 +020043#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020044
Jacob Erlbeckbc555742013-10-18 14:34:55 +020045enum gbprox_global_ctr {
46 GBPROX_GLOB_CTR_INV_BVCI,
47 GBPROX_GLOB_CTR_INV_LAC,
48 GBPROX_GLOB_CTR_INV_RAC,
49 GBPROX_GLOB_CTR_INV_NSEI,
50 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
51 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
52 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
53 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
54 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
55 GBPROX_GLOB_CTR_TX_ERR_SGSN,
56 GBPROX_GLOB_CTR_OTHER_ERR,
57};
58
59static const struct rate_ctr_desc global_ctr_description[] = {
60 { "inv-bvci", "Invalid BVC Identifier " },
61 { "inv-lac", "Invalid Location Area Code " },
62 { "inv-rac", "Invalid Routing Area Code " },
63 { "inv-nsei", "No BVC established for NSEI " },
64 { "proto-err.bss", "BSSGP protocol error (BSS )" },
65 { "proto-err.sgsn", "BSSGP protocol error (SGSN)" },
66 { "not-supp.bss", "Feature not supported (BSS )" },
67 { "not-supp.sgsn", "Feature not supported (SGSN)" },
68 { "restart.sgsn", "Restarted RESET procedure (SGSN)" },
69 { "tx-err.sgsn", "NS Transmission error (SGSN)" },
70 { "error", "Other error " },
71};
72
73static const struct rate_ctr_group_desc global_ctrg_desc = {
74 .group_name_prefix = "gbproxy.global",
75 .group_description = "GBProxy Global Statistics",
76 .num_ctr = ARRAY_SIZE(global_ctr_description),
77 .ctr_desc = global_ctr_description,
78};
79
80static struct rate_ctr_group *global_ctrg = NULL;
81
82static struct rate_ctr_group *get_global_ctrg()
83{
84 if (global_ctrg)
85 return global_ctrg;
86
87 global_ctrg = rate_ctr_group_alloc(tall_bsc_ctx, &global_ctrg_desc, 0);
88 return global_ctrg;
89}
90
91enum gbprox_peer_ctr {
92 GBPROX_PEER_CTR_BLOCKED,
93 GBPROX_PEER_CTR_UNBLOCKED,
94 GBPROX_PEER_CTR_DROPPED,
95 GBPROX_PEER_CTR_INV_NSEI,
96 GBPROX_PEER_CTR_TX_ERR,
97};
98
99static const struct rate_ctr_desc peer_ctr_description[] = {
100 { "blocked", "BVC Block " },
101 { "unblocked", "BVC Unblock " },
102 { "dropped", "BVC blocked, dropped packet " },
103 { "inv-nsei", "NSEI mismatch " },
104 { "tx-err", "NS Transmission error " },
105};
106
107static const struct rate_ctr_group_desc peer_ctrg_desc = {
108 .group_name_prefix = "gbproxy.peer",
109 .group_description = "GBProxy Peer Statistics",
110 .num_ctr = ARRAY_SIZE(peer_ctr_description),
111 .ctr_desc = peer_ctr_description,
112};
113
Harald Welte9f75c352010-04-30 20:26:32 +0200114struct gbprox_peer {
115 struct llist_head list;
116
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200117 /* NSEI of the peer entity */
118 uint16_t nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200119
120 /* BVCI used for Point-to-Point to this peer */
121 uint16_t bvci;
Harald Welte36f98d92011-02-06 13:09:29 +0100122 int blocked;
Harald Welte9f75c352010-04-30 20:26:32 +0200123
124 /* Routeing Area that this peer is part of (raw 04.08 encoding) */
125 uint8_t ra[6];
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200126
127 /* Counter */
128 struct rate_ctr_group *ctrg;
Harald Welte9f75c352010-04-30 20:26:32 +0200129};
130
131/* Linked list of all Gb peers (except SGSN) */
132static LLIST_HEAD(gbprox_bts_peers);
133
Harald Welte9f75c352010-04-30 20:26:32 +0200134/* Find the gbprox_peer by its BVCI */
135static struct gbprox_peer *peer_by_bvci(uint16_t bvci)
136{
137 struct gbprox_peer *peer;
138 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
139 if (peer->bvci == bvci)
140 return peer;
141 }
142 return NULL;
143}
144
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200145/* Find the gbprox_peer by its NSEI */
146static struct gbprox_peer *peer_by_nsei(uint16_t nsei)
Harald Welte9f75c352010-04-30 20:26:32 +0200147{
148 struct gbprox_peer *peer;
149 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200150 if (peer->nsei == nsei)
Harald Welte9f75c352010-04-30 20:26:32 +0200151 return peer;
152 }
153 return NULL;
154}
155
156/* look-up a peer by its Routeing Area Code (RAC) */
Harald Welte70f38d22010-05-01 12:10:57 +0200157static struct gbprox_peer *peer_by_rac(const uint8_t *ra)
Harald Welte9f75c352010-04-30 20:26:32 +0200158{
159 struct gbprox_peer *peer;
160 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Harald Welte1174c082010-05-12 00:07:29 +0200161 if (!memcmp(peer->ra, ra, 6))
Harald Welte9f75c352010-04-30 20:26:32 +0200162 return peer;
163 }
164 return NULL;
165}
166
167/* look-up a peer by its Location Area Code (LAC) */
Harald Welte70f38d22010-05-01 12:10:57 +0200168static struct gbprox_peer *peer_by_lac(const uint8_t *la)
Harald Welte9f75c352010-04-30 20:26:32 +0200169{
170 struct gbprox_peer *peer;
171 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Harald Welte1174c082010-05-12 00:07:29 +0200172 if (!memcmp(peer->ra, la, 5))
Harald Welte9f75c352010-04-30 20:26:32 +0200173 return peer;
174 }
175 return NULL;
176}
177
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200178static int check_peer_nsei(struct gbprox_peer *peer, uint16_t nsei)
179{
180 if (peer->nsei != nsei) {
181 LOGP(DGPRS, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
182 "BVCI=%u via NSEI=%u (expected NSEI=%u)\n",
183 peer->bvci, nsei, peer->nsei);
184 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
185 return 1;
186 }
187
188 return 0;
189}
190
Harald Welte9f75c352010-04-30 20:26:32 +0200191static struct gbprox_peer *peer_alloc(uint16_t bvci)
192{
193 struct gbprox_peer *peer;
194
195 peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer);
196 if (!peer)
197 return NULL;
198
199 peer->bvci = bvci;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200200 peer->ctrg = rate_ctr_group_alloc(peer, &peer_ctrg_desc, bvci);
201
Harald Welte9f75c352010-04-30 20:26:32 +0200202 llist_add(&peer->list, &gbprox_bts_peers);
203
204 return peer;
205}
206
Jacob Erlbeckc5085f92013-10-18 13:04:48 +0200207static void peer_free(struct gbprox_peer *peer) __attribute__((__unused__));
Harald Welte9f75c352010-04-30 20:26:32 +0200208static void peer_free(struct gbprox_peer *peer)
209{
210 llist_del(&peer->list);
211 talloc_free(peer);
212}
213
Harald Welte39d0bb52010-05-12 18:10:25 +0000214/* FIXME: this needs to go to libosmocore/msgb.c */
215static struct msgb *msgb_copy(const struct msgb *msg, const char *name)
216{
Harald Welte8645e102012-06-16 16:09:52 +0800217 struct libgb_msgb_cb *old_cb, *new_cb;
Harald Welte39d0bb52010-05-12 18:10:25 +0000218 struct msgb *new_msg;
219
Holger Hans Peter Freyther62d97982010-06-08 18:14:37 +0800220 new_msg = msgb_alloc(msg->data_len, name);
Harald Welte39d0bb52010-05-12 18:10:25 +0000221 if (!new_msg)
222 return NULL;
223
Harald Welte39d0bb52010-05-12 18:10:25 +0000224 /* copy data */
Holger Hans Peter Freyther62d97982010-06-08 18:14:37 +0800225 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
226
227 /* copy header */
228 new_msg->len = msg->len;
229 new_msg->data += msg->data - msg->_data;
230 new_msg->head += msg->head - msg->_data;
231 new_msg->tail += msg->tail - msg->_data;
232
233 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
234 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
235 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
236 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
237
238 /* copy GB specific data */
Harald Welte8645e102012-06-16 16:09:52 +0800239 old_cb = LIBGB_MSGB_CB(msg);
240 new_cb = LIBGB_MSGB_CB(new_msg);
Holger Hans Peter Freyther62d97982010-06-08 18:14:37 +0800241
242 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
243 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
244
Harald Weltefb9e06f2011-02-06 17:17:05 +0100245 /* bssgp_cell_id is a pointer into the old msgb, so we need to make
246 * it a pointer into the new msgb */
247 new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data);
Holger Hans Peter Freyther62d97982010-06-08 18:14:37 +0800248 new_cb->nsei = old_cb->nsei;
249 new_cb->bvci = old_cb->bvci;
250 new_cb->tlli = old_cb->tlli;
Harald Welte39d0bb52010-05-12 18:10:25 +0000251
252 return new_msg;
253}
254
Harald Welte69619e32010-05-03 19:05:10 +0200255/* strip off the NS header */
256static void strip_ns_hdr(struct msgb *msg)
257{
258 int strip_len = msgb_bssgph(msg) - msg->data;
259 msgb_pull(msg, strip_len);
260}
261
Harald Welte9f75c352010-04-30 20:26:32 +0200262/* feed a message down the NS-VC associated with the specified peer */
Harald Welte39d0bb52010-05-12 18:10:25 +0000263static int gbprox_relay2sgsn(struct msgb *old_msg, uint16_t ns_bvci)
Harald Welte672f5c42010-05-03 18:54:58 +0200264{
Harald Welte39d0bb52010-05-12 18:10:25 +0000265 /* create a copy of the message so the old one can
266 * be free()d safely when we return from gbprox_rcvmsg() */
267 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200268 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000269
Harald Weltee9ea2692010-05-11 20:20:13 +0200270 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Harald Welte96f71f22010-05-03 19:28:05 +0200271 msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200272
Harald Welte672f5c42010-05-03 18:54:58 +0200273 msgb_bvci(msg) = ns_bvci;
274 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
275
Harald Welte69619e32010-05-03 19:05:10 +0200276 strip_ns_hdr(msg);
277
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200278 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
279 if (rc < 0)
280 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
281
282 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200283}
284
Harald Welte672f5c42010-05-03 18:54:58 +0200285/* feed a message down the NS-VC associated with the specified peer */
Harald Welte39d0bb52010-05-12 18:10:25 +0000286static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200287 uint16_t ns_bvci)
288{
Harald Welte39d0bb52010-05-12 18:10:25 +0000289 /* create a copy of the message so the old one can
290 * be free()d safely when we return from gbprox_rcvmsg() */
291 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200292 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000293
Harald Welte0ab535b2010-05-13 10:34:56 +0200294 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200295 msgb_nsei(msg), ns_bvci, peer->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200296
Harald Welte9f75c352010-04-30 20:26:32 +0200297 msgb_bvci(msg) = ns_bvci;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200298 msgb_nsei(msg) = peer->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200299
Harald Welte0ab535b2010-05-13 10:34:56 +0200300 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200301 strip_ns_hdr(msg);
302
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200303 rc = gprs_ns_sendmsg(bssgp_nsi, msg);
304 if (rc < 0)
305 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
306
307 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200308}
309
Harald Welte36f98d92011-02-06 13:09:29 +0100310static int block_unblock_peer(uint16_t ptp_bvci, uint8_t pdu_type)
311{
312 struct gbprox_peer *peer;
313
314 peer = peer_by_bvci(ptp_bvci);
315 if (!peer) {
316 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
317 ptp_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200318 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100319 return -ENOENT;
320 }
321
322 switch (pdu_type) {
323 case BSSGP_PDUT_BVC_BLOCK_ACK:
324 peer->blocked = 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200325 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100326 break;
327 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
328 peer->blocked = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200329 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100330 break;
331 default:
332 break;
333 }
334 return 0;
335}
336
Harald Welte9f75c352010-04-30 20:26:32 +0200337/* Send a message to a peer identified by ptp_bvci but using ns_bvci
338 * in the NS hdr */
Harald Welte69619e32010-05-03 19:05:10 +0200339static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200340 uint16_t ns_bvci)
341{
342 struct gbprox_peer *peer;
343
344 peer = peer_by_bvci(ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200345 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200346 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200347 ptp_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200348 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200349 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200350 }
Harald Welte9f75c352010-04-30 20:26:32 +0200351
Harald Welte69619e32010-05-03 19:05:10 +0200352 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200353}
354
Harald Welteb1fd9022012-06-17 12:16:31 +0800355int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
356{
357 return 0;
358}
359
Harald Welte9f75c352010-04-30 20:26:32 +0200360/* Receive an incoming signalling message from a BSS-side NS-VC */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200361static int gbprox_rx_sig_from_bss(struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200362 uint16_t ns_bvci)
363{
Harald Welteca3620a2010-05-03 16:30:59 +0200364 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200365 struct tlv_parsed tp;
366 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200367 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200368 struct gbprox_peer *from_peer;
Harald Welte70f38d22010-05-01 12:10:57 +0200369 struct gprs_ra_id raid;
Harald Welte9f75c352010-04-30 20:26:32 +0200370
Harald Weltec471d3d2011-02-06 17:13:12 +0100371 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200372 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200373 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200374 return -EINVAL;
375 }
376
377 /* we actually should never see those two for BVCI == 0, but double-check
378 * just to make sure */
379 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
380 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200381 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200382 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200383 return -EINVAL;
384 }
385
386 bssgp_tlv_parse(&tp, bgph->data, data_len);
387
388 switch (pdu_type) {
389 case BSSGP_PDUT_SUSPEND:
390 case BSSGP_PDUT_RESUME:
Harald Welte70f38d22010-05-01 12:10:57 +0200391 /* We implement RAC snooping during SUSPEND/RESUME, since
392 * it establishes a relationsip between BVCI/peer and the
393 * routeing area code. The snooped information is then
394 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
395 * the correct BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200396 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
397 goto err_mand_ie;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200398 from_peer = peer_by_nsei(nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200399 if (!from_peer)
400 goto err_no_peer;
Harald Welte1174c082010-05-12 00:07:29 +0200401 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
402 sizeof(from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200403 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte4cf12e92010-05-13 14:14:56 +0200404 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200405 "RAC snooping: RAC %u-%u-%u-%u behind BVCI=%u\n",
406 nsei, raid.mcc, raid.mnc, raid.lac,
407 raid.rac , from_peer->bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200408 /* FIXME: This only supports one BSS per RA */
409 break;
Harald Welte44c48302010-05-03 19:22:32 +0200410 case BSSGP_PDUT_BVC_RESET:
411 /* If we receive a BVC reset on the signalling endpoint, we
412 * don't want the SGSN to reset, as the signalling endpoint
413 * is common for all point-to-point BVCs (and thus all BTS) */
414 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200415 uint16_t bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte72953b82010-05-12 00:20:41 +0200416 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200417 nsei, bvci);
Harald Welte44c48302010-05-03 19:22:32 +0200418 if (bvci == 0) {
419 /* FIXME: only do this if SGSN is alive! */
Harald Weltee9ea2692010-05-11 20:20:13 +0200420 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200421 "BVC RESET ACK of BVCI=0\n", nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200422 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200423 nsei, 0, ns_bvci);
Harald Welte1174c082010-05-12 00:07:29 +0200424 }
425 from_peer = peer_by_bvci(bvci);
426 if (!from_peer) {
Harald Welte1c77c6e2010-05-03 21:37:11 +0200427 /* if a PTP-BVC is reset, and we don't know that
428 * PTP-BVCI yet, we should allocate a new peer */
429 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200430 "BVCI=%u via NSEI=%u\n", bvci, nsei);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200431 from_peer = peer_alloc(bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200432 from_peer->nsei = nsei;
Harald Welte44c48302010-05-03 19:22:32 +0200433 }
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200434 check_peer_nsei(from_peer, nsei);
Harald Welte1174c082010-05-12 00:07:29 +0200435 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
436 struct gprs_ra_id raid;
437 /* We have a Cell Identifier present in this
438 * PDU, this means we can extend our local
439 * state information about this particular cell
440 * */
441 memcpy(from_peer->ra,
442 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
443 sizeof(from_peer->ra));
444 gsm48_parse_ra(&raid, from_peer->ra);
445 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200446 "Cell ID %u-%u-%u-%u\n", nsei,
Harald Welte1174c082010-05-12 00:07:29 +0200447 bvci, raid.mcc, raid.mnc, raid.lac,
448 raid.rac);
449 }
Harald Welte44c48302010-05-03 19:22:32 +0200450 }
451 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200452 }
453
Harald Weltee9ea2692010-05-11 20:20:13 +0200454 /* Normally, we can simply pass on all signalling messages from BSS to
455 * SGSN */
Harald Welte69619e32010-05-03 19:05:10 +0200456 return gbprox_relay2sgsn(msg, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200457err_no_peer:
Jacob Erlbeckc5085f92013-10-18 13:04:48 +0200458 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200459 nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200460 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200461 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200462err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200463 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200464 nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200465 rate_ctr_inc(&get_global_ctrg()->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200466 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200467}
468
469/* Receive paging request from SGSN, we need to relay to proper BSS */
470static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200471 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200472{
Harald Welte4cf12e92010-05-13 14:14:56 +0200473 struct gbprox_peer *peer = NULL;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200474 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte9f75c352010-04-30 20:26:32 +0200475
Harald Welte4cf12e92010-05-13 14:14:56 +0200476 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200477 nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200478 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200479 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte4cf12e92010-05-13 14:14:56 +0200480 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
481 bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200482 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte9f75c352010-04-30 20:26:32 +0200483 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
484 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte4cf12e92010-05-13 14:14:56 +0200485 LOGPC(DGPRS, LOGL_INFO, "routing by RAC to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800486 peer ? peer->bvci : -1);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200487 errctr = GBPROX_GLOB_CTR_INV_RAC;
Harald Welte9f75c352010-04-30 20:26:32 +0200488 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
489 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Harald Welte4cf12e92010-05-13 14:14:56 +0200490 LOGPC(DGPRS, LOGL_INFO, "routing by LAC to peer BVCI=%u\n",
Holger Hans Peter Freyther62eaf542010-06-08 16:30:24 +0800491 peer ? peer->bvci : -1);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200492 errctr = GBPROX_GLOB_CTR_INV_LAC;
Harald Welte9f75c352010-04-30 20:26:32 +0200493 } else
Harald Welte4cf12e92010-05-13 14:14:56 +0200494 LOGPC(DGPRS, LOGL_INFO, "\n");
495
496 if (!peer) {
497 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200498 "unable to route, missing IE\n", nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200499 rate_ctr_inc(&get_global_ctrg()->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +0200500 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +0200501 }
502 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200503}
504
Harald Welte0a4050c2010-05-11 10:01:17 +0200505/* Receive an incoming BVC-RESET message from the SGSN */
506static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200507 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +0200508{
509 struct gbprox_peer *peer;
510 uint16_t ptp_bvci;
511
512 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200513 rate_ctr_inc(&get_global_ctrg()->
514 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200515 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
516 NULL, msg);
517 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200518 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +0200519
520 if (ptp_bvci >= 2) {
521 /* A reset for a PTP BVC was received, forward it to its
522 * respective peer */
523 peer = peer_by_bvci(ptp_bvci);
524 if (!peer) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200525 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200526 nsei, ptp_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200527 rate_ctr_inc(&get_global_ctrg()->
528 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200529 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
530 NULL, msg);
531 }
532 return gbprox_relay2peer(msg, peer, ns_bvci);
533 }
534
535 /* A reset for the Signalling entity has been received
536 * from the SGSN. As the signalling BVCI is shared
537 * among all the BSS's that we multiplex, it needs to
538 * be relayed */
539 llist_for_each_entry(peer, &gbprox_bts_peers, list)
540 gbprox_relay2peer(msg, peer, ns_bvci);
541
542 return 0;
543}
544
Harald Welte9f75c352010-04-30 20:26:32 +0200545/* Receive an incoming signalling message from the SGSN-side NS-VC */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200546static int gbprox_rx_sig_from_sgsn(struct msgb *msg, uint32_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200547 uint16_t ns_bvci)
548{
Harald Welteca3620a2010-05-03 16:30:59 +0200549 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200550 struct tlv_parsed tp;
551 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200552 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200553 struct gbprox_peer *peer;
554 uint16_t bvci;
555 int rc = 0;
556
Harald Weltec471d3d2011-02-06 17:13:12 +0100557 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte4cf12e92010-05-13 14:14:56 +0200558 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200559 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200560 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +0200561 return -EINVAL;
562 }
563
564 /* we actually should never see those two for BVCI == 0, but double-check
565 * just to make sure */
566 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
567 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200568 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200569 "signalling\n", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200570 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200571 }
572
573 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
574
575 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +0200576 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200577 rc = rx_reset_from_sgsn(msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200578 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200579 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +0200580 case BSSGP_PDUT_BVC_RESET_ACK:
581 /* simple case: BVCI IE is mandatory */
582 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
583 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200584 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte69619e32010-05-03 19:05:10 +0200585 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200586 break;
587 case BSSGP_PDUT_PAGING_PS:
588 case BSSGP_PDUT_PAGING_CS:
589 /* process the paging request (LAC/RAC lookup) */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200590 rc = gbprox_rx_paging(msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200591 break;
592 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +0200593 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +0200594 LOGP(DGPRS, LOGL_NOTICE,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200595 "NSEI=%u(SGSN) BSSGP STATUS ", nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200596 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
597 LOGPC(DGPRS, LOGL_NOTICE, "\n");
598 goto err_mand_ie;
599 }
600 LOGPC(DGPRS, LOGL_NOTICE,
601 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
602 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
603 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200604 uint16_t bvci = tlvp_val16_unal(&tp, BSSGP_IE_BVCI);
Harald Welte0a4050c2010-05-11 10:01:17 +0200605 LOGPC(DGPRS, LOGL_NOTICE,
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200606 "BVCI=%u\n", ntohs(bvci));
Harald Welte0a4050c2010-05-11 10:01:17 +0200607 } else
608 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +0200609 break;
610 /* those only exist in the SGSN -> BSS direction */
611 case BSSGP_PDUT_SUSPEND_ACK:
612 case BSSGP_PDUT_SUSPEND_NACK:
613 case BSSGP_PDUT_RESUME_ACK:
614 case BSSGP_PDUT_RESUME_NACK:
615 /* RAC IE is mandatory */
616 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
617 goto err_mand_ie;
618 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
619 if (!peer)
620 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +0200621 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200622 break;
Harald Welte36f98d92011-02-06 13:09:29 +0100623 case BSSGP_PDUT_BVC_BLOCK_ACK:
624 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
625 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
626 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200627 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +0100628 if (bvci == 0) {
629 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200630 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +0100631 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
632 /* should we send STATUS ? */
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200633 rate_ctr_inc(&get_global_ctrg()->
634 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100635 } else {
636 /* Mark BVC as (un)blocked */
637 block_unblock_peer(bvci, pdu_type);
638 }
639 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
640 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200641 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +0200642 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200643 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200644 rate_ctr_inc(&get_global_ctrg()->
645 ctr[GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200646 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200647 break;
648 default:
Harald Welte72953b82010-05-12 00:20:41 +0200649 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
650 pdu_type);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200651 rate_ctr_inc(&get_global_ctrg()->
652 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200653 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200654 break;
655 }
656
657 return rc;
658err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +0200659 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200660 nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200661 rate_ctr_inc(&get_global_ctrg()->
662 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200663 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200664err_no_peer:
Harald Welte0a4050c2010-05-11 10:01:17 +0200665 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200666 nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200667 rate_ctr_inc(&get_global_ctrg()-> ctr[GBPROX_GLOB_CTR_INV_RAC]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200668 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200669}
670
671/* Main input function for Gb proxy */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200672int gbprox_rcvmsg(struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200673{
Harald Welte672f5c42010-05-03 18:54:58 +0200674 int rc;
Harald Welte36f98d92011-02-06 13:09:29 +0100675 struct gbprox_peer *peer;
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200676 int remote_end_is_sgsn = nsei == gbcfg.nsip_sgsn_nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200677
678 /* Only BVCI=0 messages need special treatment */
679 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200680 if (remote_end_is_sgsn)
681 rc = gbprox_rx_sig_from_sgsn(msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200682 else
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200683 rc = gbprox_rx_sig_from_bss(msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +0200684 } else {
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200685 peer = peer_by_bvci(ns_bvci);
686
Harald Welte672f5c42010-05-03 18:54:58 +0200687 /* All other BVCI are PTP and thus can be simply forwarded */
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200688 if (!remote_end_is_sgsn) {
689 if (peer)
690 check_peer_nsei(peer, nsei);
Harald Welte36f98d92011-02-06 13:09:29 +0100691 return gbprox_relay2sgsn(msg, ns_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200692 }
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200693
Harald Welte36f98d92011-02-06 13:09:29 +0100694 /* else: SGSN -> BSS direction */
Harald Welte36f98d92011-02-06 13:09:29 +0100695 if (!peer) {
696 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
697 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200698 nsvci, nsei);
Harald Welte36f98d92011-02-06 13:09:29 +0100699 peer = peer_alloc(ns_bvci);
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200700 peer->nsei = nsei;
Harald Welte36f98d92011-02-06 13:09:29 +0100701 }
702 if (peer->blocked) {
703 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
704 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200705 ns_bvci, nsvci, nsei);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200706 rate_ctr_inc(&peer->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100707 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, NULL, msg);
708 }
709 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200710 }
711
Harald Welte672f5c42010-05-03 18:54:58 +0200712 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200713}
Harald Welte85801d02010-05-11 05:49:43 +0200714
Harald Welte1ccbf442010-05-14 11:53:08 +0000715int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
716{
717 struct gprs_nsvc *nsvc;
718
719 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
720 if (!nsvc->persistent)
721 continue;
722 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
723 }
724 return 0;
725}
726
Harald Weltec1c1dd22010-05-11 06:34:24 +0200727/* Signal handler for signals from NS layer */
728int gbprox_signal(unsigned int subsys, unsigned int signal,
729 void *handler_data, void *signal_data)
730{
731 struct ns_signal_data *nssd = signal_data;
732 struct gprs_nsvc *nsvc = nssd->nsvc;
733 struct gbprox_peer *peer;
734
Harald Weltea6a20b42012-06-16 16:40:42 +0800735 if (subsys != SS_L_NS)
Harald Weltec1c1dd22010-05-11 06:34:24 +0200736 return 0;
737
Harald Weltef69c0592010-05-11 18:29:44 +0200738 if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) {
739 /* We have received a NS-RESET from the NSEI and NSVC
740 * of the SGSN. This might happen with SGSN that start
741 * their own NS-RESET procedure without waiting for our
742 * NS-RESET */
743 nsvc->remote_end_is_sgsn = 1;
744 }
745
Harald Welteb778d2c2010-05-12 13:28:25 +0000746 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
747 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
748 "re-starting RESET procedure\n");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200749 rate_ctr_inc(&get_global_ctrg()->
750 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
Harald Weltee6599ee2012-06-17 12:25:53 +0800751 gprs_ns_nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr,
752 nsvc->nsei, nsvc->nsvci);
Harald Welteb778d2c2010-05-12 13:28:25 +0000753 }
754
Harald Welte5e106d72011-02-06 16:33:29 +0100755 if (!nsvc->remote_end_is_sgsn) {
756 /* from BSS to SGSN */
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200757 peer = peer_by_nsei(nsvc->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +0100758 if (!peer) {
759 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
760 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
761 nsvc->nsvci);
762 return 0;
763 }
Harald Weltec1c1dd22010-05-11 06:34:24 +0200764 switch (signal) {
765 case S_NS_RESET:
Harald Weltec1c1dd22010-05-11 06:34:24 +0200766 case S_NS_BLOCK:
Harald Welte5e106d72011-02-06 16:33:29 +0100767 if (!peer->blocked)
768 break;
769 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
770 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
771 nsvc->nsei, nsvc->nsvci);
772 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
773 peer->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +0200774 break;
Harald Welte5e106d72011-02-06 16:33:29 +0100775 }
776 } else {
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200777 /* Forward this message to all NS-VC to BSS */
778 struct gprs_ns_inst *nsi = gbcfg.nsi;
779 struct gprs_nsvc *next_nsvc;
780
781 llist_for_each_entry(next_nsvc, &nsi->gprs_nsvcs, list) {
782 if (next_nsvc->remote_end_is_sgsn)
783 continue;
784
785 /* Note that the following does not start the full
786 * procedures including timer based retransmissions. */
Harald Welte5e106d72011-02-06 16:33:29 +0100787 switch (signal) {
788 case S_NS_RESET:
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200789 gprs_ns_tx_reset(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +0100790 break;
791 case S_NS_BLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200792 gprs_ns_tx_block(next_nsvc, nssd->cause);
Harald Welte5e106d72011-02-06 16:33:29 +0100793 break;
794 case S_NS_UNBLOCK:
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200795 gprs_ns_tx_unblock(next_nsvc);
Harald Welte5e106d72011-02-06 16:33:29 +0100796 break;
797 }
Harald Weltec1c1dd22010-05-11 06:34:24 +0200798 }
799 }
800 return 0;
801}
802
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200803int gbprox_dump_peers(FILE *stream, int indent, int verbose)
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200804{
805 struct gbprox_peer *peer;
806 struct gprs_ra_id raid;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200807 unsigned int i;
808 const struct rate_ctr_group_desc *desc;
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200809 int rc;
810
811 fprintf(stream, "%*sPeers:\n", indent, "");
812 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
813 gsm48_parse_ra(&raid, peer->ra);
814
815 rc = fprintf(stream, "%*s NSEI %u, BVCI %u, %sblocked, "
816 "RAC %u-%u-%u-%u\n",
817 indent, "",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200818 peer->nsei, peer->bvci,
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200819 peer->blocked ? "" : "not ",
820 raid.mcc, raid.mnc, raid.lac, raid.rac);
821
822 if (rc < 0)
823 return rc;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200824
825 if (!verbose)
826 continue;
827
828 desc = peer->ctrg->desc;
829
830 for (i = 0; i < desc->num_ctr; i++) {
831 struct rate_ctr *ctr = &peer->ctrg->ctr[i];
832 if (ctr->current) {
833 rc = fprintf(stream, "%*s %s: %llu\n",
834 indent, "",
835 desc->ctr_desc[i].description,
836 (long long)ctr->current);
837
838 if (rc < 0)
839 return rc;
840 }
841 }
Jacob Erlbeck51a869c2013-10-15 12:00:26 +0200842 }
843
844 return 0;
845}
Harald Welte85801d02010-05-11 05:49:43 +0200846
Harald Welte4b037e42010-05-19 19:45:32 +0200847#include <osmocom/vty/command.h>
Harald Welte85801d02010-05-11 05:49:43 +0200848
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200849gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy [stats]",
Harald Welte85801d02010-05-11 05:49:43 +0200850 SHOW_STR "Display information about the Gb proxy")
851{
852 struct gbprox_peer *peer;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200853 int show_stats = argc >= 1;
854
855 if (show_stats)
856 vty_out_rate_ctr_group(vty, "", get_global_ctrg());
Harald Welte85801d02010-05-11 05:49:43 +0200857
858 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Harald Welte85801d02010-05-11 05:49:43 +0200859 struct gprs_ra_id raid;
Harald Welte7fc98222010-05-11 10:15:42 +0200860 gsm48_parse_ra(&raid, peer->ra);
Harald Welte85801d02010-05-11 05:49:43 +0200861
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200862 vty_out(vty, "NSEI %5u, PTP-BVCI %5u, "
Harald Welte7570c212010-05-19 17:06:16 +0200863 "RAC %u-%u-%u-%u",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200864 peer->nsei, peer->bvci,
Harald Welte7570c212010-05-19 17:06:16 +0200865 raid.mcc, raid.mnc, raid.lac, raid.rac);
Harald Weltef3956cb2011-02-06 17:44:14 +0100866 if (peer->blocked)
867 vty_out(vty, " [BVC-BLOCKED]");
868
869 vty_out(vty, "%s", VTY_NEWLINE);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200870
871 if (show_stats)
872 vty_out_rate_ctr_group(vty, " ", peer->ctrg);
Harald Welte85801d02010-05-11 05:49:43 +0200873 }
874 return CMD_SUCCESS;
875}