blob: 1261cccaeebb13208cf43147edf74d93419cc7a2 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* NS-over-IP proxy */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by On-Waves
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * 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
10 * (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
15 * GNU Affero General Public License for more details.
16 *
17 * 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/>.
19 *
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>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040030#include <arpa/inet.h>
31
Jonathan Santos5a45b152011-08-17 15:33:57 -040032#include <osmocom/core/talloc.h>
33#include <osmocom/core/select.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040034
35#include <openbsc/signal.h>
36#include <openbsc/debug.h>
37#include <openbsc/gprs_ns.h>
38#include <openbsc/gprs_bssgp.h>
39#include <openbsc/gb_proxy.h>
40
41struct gbprox_peer {
42 struct llist_head list;
43
44 /* NS-VC over which we send/receive data to this BVC */
45 struct gprs_nsvc *nsvc;
46
47 /* BVCI used for Point-to-Point to this peer */
48 uint16_t bvci;
49 int blocked;
50
51 /* Routeing Area that this peer is part of (raw 04.08 encoding) */
52 uint8_t ra[6];
53};
54
55/* Linked list of all Gb peers (except SGSN) */
56static LLIST_HEAD(gbprox_bts_peers);
57
58/* Find the gbprox_peer by its BVCI */
59static struct gbprox_peer *peer_by_bvci(uint16_t bvci)
60{
61 struct gbprox_peer *peer;
62 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
63 if (peer->bvci == bvci)
64 return peer;
65 }
66 return NULL;
67}
68
69static struct gbprox_peer *peer_by_nsvc(struct gprs_nsvc *nsvc)
70{
71 struct gbprox_peer *peer;
72 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
73 if (peer->nsvc == nsvc)
74 return peer;
75 }
76 return NULL;
77}
78
79/* look-up a peer by its Routeing Area Code (RAC) */
80static struct gbprox_peer *peer_by_rac(const uint8_t *ra)
81{
82 struct gbprox_peer *peer;
83 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
84 if (!memcmp(peer->ra, ra, 6))
85 return peer;
86 }
87 return NULL;
88}
89
90/* look-up a peer by its Location Area Code (LAC) */
91static struct gbprox_peer *peer_by_lac(const uint8_t *la)
92{
93 struct gbprox_peer *peer;
94 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
95 if (!memcmp(peer->ra, la, 5))
96 return peer;
97 }
98 return NULL;
99}
100
101static struct gbprox_peer *peer_alloc(uint16_t bvci)
102{
103 struct gbprox_peer *peer;
104
105 peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer);
106 if (!peer)
107 return NULL;
108
109 peer->bvci = bvci;
110 llist_add(&peer->list, &gbprox_bts_peers);
111
112 return peer;
113}
114
115static void peer_free(struct gbprox_peer *peer)
116{
117 llist_del(&peer->list);
118 talloc_free(peer);
119}
120
121/* FIXME: this needs to go to libosmocore/msgb.c */
122static struct msgb *msgb_copy(const struct msgb *msg, const char *name)
123{
124 struct openbsc_msgb_cb *old_cb, *new_cb;
125 struct msgb *new_msg;
126
127 new_msg = msgb_alloc(msg->data_len, name);
128 if (!new_msg)
129 return NULL;
130
131 /* copy data */
132 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
133
134 /* copy header */
135 new_msg->len = msg->len;
136 new_msg->data += msg->data - msg->_data;
137 new_msg->head += msg->head - msg->_data;
138 new_msg->tail += msg->tail - msg->_data;
139
140 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
141 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
142 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
143 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
144
145 /* copy GB specific data */
146 old_cb = OBSC_MSGB_CB(msg);
147 new_cb = OBSC_MSGB_CB(new_msg);
148
149 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
150 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
151
152 /* bssgp_cell_id is a pointer into the old msgb, so we need to make
153 * it a pointer into the new msgb */
154 new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data);
155 new_cb->nsei = old_cb->nsei;
156 new_cb->bvci = old_cb->bvci;
157 new_cb->tlli = old_cb->tlli;
158
159 return new_msg;
160}
161
162/* strip off the NS header */
163static void strip_ns_hdr(struct msgb *msg)
164{
165 int strip_len = msgb_bssgph(msg) - msg->data;
166 msgb_pull(msg, strip_len);
167}
168
169/* feed a message down the NS-VC associated with the specified peer */
170static int gbprox_relay2sgsn(struct msgb *old_msg, uint16_t ns_bvci)
171{
172 /* create a copy of the message so the old one can
173 * be free()d safely when we return from gbprox_rcvmsg() */
174 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn");
175
176 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
177 msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei);
178
179 msgb_bvci(msg) = ns_bvci;
180 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
181
182 strip_ns_hdr(msg);
183
184 return gprs_ns_sendmsg(bssgp_nsi, msg);
185}
186
187/* feed a message down the NS-VC associated with the specified peer */
188static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer,
189 uint16_t ns_bvci)
190{
191 /* create a copy of the message so the old one can
192 * be free()d safely when we return from gbprox_rcvmsg() */
193 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer");
194
195 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
196 msgb_nsei(msg), ns_bvci, peer->nsvc->nsei);
197
198 msgb_bvci(msg) = ns_bvci;
199 msgb_nsei(msg) = peer->nsvc->nsei;
200
201 /* Strip the old NS header, it will be replaced with a new one */
202 strip_ns_hdr(msg);
203
204 return gprs_ns_sendmsg(bssgp_nsi, msg);
205}
206
207static int block_unblock_peer(uint16_t ptp_bvci, uint8_t pdu_type)
208{
209 struct gbprox_peer *peer;
210
211 peer = peer_by_bvci(ptp_bvci);
212 if (!peer) {
213 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
214 ptp_bvci);
215 return -ENOENT;
216 }
217
218 switch (pdu_type) {
219 case BSSGP_PDUT_BVC_BLOCK_ACK:
220 peer->blocked = 1;
221 break;
222 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
223 peer->blocked = 0;
224 break;
225 default:
226 break;
227 }
228 return 0;
229}
230
231/* Send a message to a peer identified by ptp_bvci but using ns_bvci
232 * in the NS hdr */
233static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
234 uint16_t ns_bvci)
235{
236 struct gbprox_peer *peer;
237
238 peer = peer_by_bvci(ptp_bvci);
239 if (!peer) {
240 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
241 ptp_bvci);
242 return -ENOENT;
243 }
244
245 return gbprox_relay2peer(msg, peer, ns_bvci);
246}
247
248/* Receive an incoming signalling message from a BSS-side NS-VC */
249static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc,
250 uint16_t ns_bvci)
251{
252 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
253 struct tlv_parsed tp;
254 uint8_t pdu_type = bgph->pdu_type;
255 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
256 struct gbprox_peer *from_peer;
257 struct gprs_ra_id raid;
258
259 if (ns_bvci != 0 && ns_bvci != 1) {
260 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
261 nsvc->nsei, ns_bvci);
262 return -EINVAL;
263 }
264
265 /* we actually should never see those two for BVCI == 0, but double-check
266 * just to make sure */
267 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
268 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
269 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
270 "signalling\n", nsvc->nsei);
271 return -EINVAL;
272 }
273
274 bssgp_tlv_parse(&tp, bgph->data, data_len);
275
276 switch (pdu_type) {
277 case BSSGP_PDUT_SUSPEND:
278 case BSSGP_PDUT_RESUME:
279 /* We implement RAC snooping during SUSPEND/RESUME, since
280 * it establishes a relationsip between BVCI/peer and the
281 * routeing area code. The snooped information is then
282 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
283 * the correct BSSGP */
284 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
285 goto err_mand_ie;
286 from_peer = peer_by_nsvc(nsvc);
287 if (!from_peer)
288 goto err_no_peer;
289 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
290 sizeof(from_peer->ra));
291 gsm48_parse_ra(&raid, from_peer->ra);
292 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
293 "RAC snooping: RAC %u-%u-%u-%u behind BVCI=%u, "
294 "NSVCI=%u\n",nsvc->nsei, raid.mcc, raid.mnc, raid.lac,
295 raid.rac , from_peer->bvci, nsvc->nsvci);
296 /* FIXME: This only supports one BSS per RA */
297 break;
298 case BSSGP_PDUT_BVC_RESET:
299 /* If we receive a BVC reset on the signalling endpoint, we
300 * don't want the SGSN to reset, as the signalling endpoint
301 * is common for all point-to-point BVCs (and thus all BTS) */
302 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
303 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
304 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
305 nsvc->nsei, bvci);
306 if (bvci == 0) {
307 /* FIXME: only do this if SGSN is alive! */
308 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
309 "BVC RESET ACK of BVCI=0\n", nsvc->nsei);
310 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
311 nsvc->nsei, 0, ns_bvci);
312 }
313 from_peer = peer_by_bvci(bvci);
314 if (!from_peer) {
315 /* if a PTP-BVC is reset, and we don't know that
316 * PTP-BVCI yet, we should allocate a new peer */
317 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
318 "BVCI=%u via NSVCI=%u/NSEI=%u\n", bvci,
319 nsvc->nsvci, nsvc->nsei);
320 from_peer = peer_alloc(bvci);
321 from_peer->nsvc = nsvc;
322 }
323 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
324 struct gprs_ra_id raid;
325 /* We have a Cell Identifier present in this
326 * PDU, this means we can extend our local
327 * state information about this particular cell
328 * */
329 memcpy(from_peer->ra,
330 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
331 sizeof(from_peer->ra));
332 gsm48_parse_ra(&raid, from_peer->ra);
333 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
334 "Cell ID %u-%u-%u-%u\n", nsvc->nsei,
335 bvci, raid.mcc, raid.mnc, raid.lac,
336 raid.rac);
337 }
338 }
339 break;
340 }
341
342 /* Normally, we can simply pass on all signalling messages from BSS to
343 * SGSN */
344 return gbprox_relay2sgsn(msg, ns_bvci);
345err_no_peer:
346 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on RAC\n",
347 nsvc->nsei);
348 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
349err_mand_ie:
350 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
351 nsvc->nsei);
352 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
353}
354
355/* Receive paging request from SGSN, we need to relay to proper BSS */
356static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
357 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
358{
359 struct gbprox_peer *peer = NULL;
360
361 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
362 nsvc->nsei);
363 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
364 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
365 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
366 bvci);
367 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
368 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
369 LOGPC(DGPRS, LOGL_INFO, "routing by RAC to peer BVCI=%u\n",
370 peer ? peer->bvci : -1);
371 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
372 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
373 LOGPC(DGPRS, LOGL_INFO, "routing by LAC to peer BVCI=%u\n",
374 peer ? peer->bvci : -1);
375 } else
376 LOGPC(DGPRS, LOGL_INFO, "\n");
377
378 if (!peer) {
379 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
380 "unable to route, missing IE\n", nsvc->nsei);
381 return -EINVAL;
382 }
383 return gbprox_relay2peer(msg, peer, ns_bvci);
384}
385
386/* Receive an incoming BVC-RESET message from the SGSN */
387static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp,
388 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
389{
390 struct gbprox_peer *peer;
391 uint16_t ptp_bvci;
392
393 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
394 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
395 NULL, msg);
396 }
397 ptp_bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
398
399 if (ptp_bvci >= 2) {
400 /* A reset for a PTP BVC was received, forward it to its
401 * respective peer */
402 peer = peer_by_bvci(ptp_bvci);
403 if (!peer) {
404 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
405 nsvc->nsei, ptp_bvci);
406 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
407 NULL, msg);
408 }
409 return gbprox_relay2peer(msg, peer, ns_bvci);
410 }
411
412 /* A reset for the Signalling entity has been received
413 * from the SGSN. As the signalling BVCI is shared
414 * among all the BSS's that we multiplex, it needs to
415 * be relayed */
416 llist_for_each_entry(peer, &gbprox_bts_peers, list)
417 gbprox_relay2peer(msg, peer, ns_bvci);
418
419 return 0;
420}
421
422/* Receive an incoming signalling message from the SGSN-side NS-VC */
423static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc,
424 uint16_t ns_bvci)
425{
426 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
427 struct tlv_parsed tp;
428 uint8_t pdu_type = bgph->pdu_type;
429 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
430 struct gbprox_peer *peer;
431 uint16_t bvci;
432 int rc = 0;
433
434 if (ns_bvci != 0 && ns_bvci != 1) {
435 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
436 "signalling\n", nsvc->nsei, ns_bvci);
437 /* FIXME: Send proper error message */
438 return -EINVAL;
439 }
440
441 /* we actually should never see those two for BVCI == 0, but double-check
442 * just to make sure */
443 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
444 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
445 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
446 "signalling\n", nsvc->nsei);
447 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
448 }
449
450 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
451
452 switch (pdu_type) {
453 case BSSGP_PDUT_BVC_RESET:
454 rc = rx_reset_from_sgsn(msg, &tp, nsvc, ns_bvci);
455 break;
456 case BSSGP_PDUT_FLUSH_LL:
457 case BSSGP_PDUT_BVC_RESET_ACK:
458 /* simple case: BVCI IE is mandatory */
459 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
460 goto err_mand_ie;
461 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
462 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
463 break;
464 case BSSGP_PDUT_PAGING_PS:
465 case BSSGP_PDUT_PAGING_CS:
466 /* process the paging request (LAC/RAC lookup) */
467 rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci);
468 break;
469 case BSSGP_PDUT_STATUS:
470 /* Some exception has occurred */
471 LOGP(DGPRS, LOGL_NOTICE,
472 "NSEI=%u(SGSN) BSSGP STATUS ", nsvc->nsei);
473 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
474 LOGPC(DGPRS, LOGL_NOTICE, "\n");
475 goto err_mand_ie;
476 }
477 LOGPC(DGPRS, LOGL_NOTICE,
478 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
479 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
480 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
481 uint16_t *bvci = (uint16_t *)
482 TLVP_VAL(&tp, BSSGP_IE_BVCI);
483 LOGPC(DGPRS, LOGL_NOTICE,
484 "BVCI=%u\n", ntohs(*bvci));
485 } else
486 LOGPC(DGPRS, LOGL_NOTICE, "\n");
487 break;
488 /* those only exist in the SGSN -> BSS direction */
489 case BSSGP_PDUT_SUSPEND_ACK:
490 case BSSGP_PDUT_SUSPEND_NACK:
491 case BSSGP_PDUT_RESUME_ACK:
492 case BSSGP_PDUT_RESUME_NACK:
493 /* RAC IE is mandatory */
494 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
495 goto err_mand_ie;
496 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
497 if (!peer)
498 goto err_no_peer;
499 rc = gbprox_relay2peer(msg, peer, ns_bvci);
500 break;
501 case BSSGP_PDUT_BVC_BLOCK_ACK:
502 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
503 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
504 goto err_mand_ie;
505 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
506 if (bvci == 0) {
507 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BSSGP "
508 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsvc->nsei,
509 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
510 /* should we send STATUS ? */
511 } else {
512 /* Mark BVC as (un)blocked */
513 block_unblock_peer(bvci, pdu_type);
514 }
515 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
516 break;
517 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
518 LOGP(DGPRS, LOGL_ERROR,
519 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsvc->nsei);
520 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
521 break;
522 default:
523 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
524 pdu_type);
525 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
526 break;
527 }
528
529 return rc;
530err_mand_ie:
531 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
532 nsvc->nsei);
533 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
534err_no_peer:
535 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n",
536 nsvc->nsei);
537 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
538}
539
540/* Main input function for Gb proxy */
541int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci)
542{
543 int rc;
544 struct gbprox_peer *peer;
545
546 /* Only BVCI=0 messages need special treatment */
547 if (ns_bvci == 0 || ns_bvci == 1) {
548 if (nsvc->remote_end_is_sgsn)
549 rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci);
550 else
551 rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
552 } else {
553 /* All other BVCI are PTP and thus can be simply forwarded */
554 if (!nsvc->remote_end_is_sgsn) {
555 return gbprox_relay2sgsn(msg, ns_bvci);
556 }
557 /* else: SGSN -> BSS direction */
558 peer = peer_by_bvci(ns_bvci);
559 if (!peer) {
560 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
561 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
562 nsvc->nsvci, nsvc->nsei);
563 peer = peer_alloc(ns_bvci);
564 peer->nsvc = nsvc;
565 }
566 if (peer->blocked) {
567 LOGP(DGPRS, LOGL_NOTICE, "Dropping PDU for "
568 "blocked BVCI=%u via NSVC=%u/NSEI=%u\n",
569 ns_bvci, nsvc->nsvci, nsvc->nsei);
570 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, NULL, msg);
571 }
572 rc = gbprox_relay2peer(msg, peer, ns_bvci);
573 }
574
575 return rc;
576}
577
578int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
579{
580 struct gprs_nsvc *nsvc;
581
582 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
583 if (!nsvc->persistent)
584 continue;
585 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
586 }
587 return 0;
588}
589
590/* Signal handler for signals from NS layer */
591int gbprox_signal(unsigned int subsys, unsigned int signal,
592 void *handler_data, void *signal_data)
593{
594 struct ns_signal_data *nssd = signal_data;
595 struct gprs_nsvc *nsvc = nssd->nsvc;
596 struct gbprox_peer *peer;
597
598 if (subsys != SS_NS)
599 return 0;
600
601 if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) {
602 /* We have received a NS-RESET from the NSEI and NSVC
603 * of the SGSN. This might happen with SGSN that start
604 * their own NS-RESET procedure without waiting for our
605 * NS-RESET */
606 nsvc->remote_end_is_sgsn = 1;
607 }
608
609 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
610 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
611 "re-starting RESET procedure\n");
612 nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, nsvc->nsei,
613 nsvc->nsvci);
614 }
615
616 if (!nsvc->remote_end_is_sgsn) {
617 /* from BSS to SGSN */
618 peer = peer_by_nsvc(nsvc);
619 if (!peer) {
620 LOGP(DGPRS, LOGL_NOTICE, "signal %u for unknown peer "
621 "NSEI=%u/NSVCI=%u\n", signal, nsvc->nsei,
622 nsvc->nsvci);
623 return 0;
624 }
625 switch (signal) {
626 case S_NS_RESET:
627 case S_NS_BLOCK:
628 if (!peer->blocked)
629 break;
630 LOGP(DGPRS, LOGL_NOTICE, "Converting NS_RESET from "
631 "NSEI=%u/NSVCI=%u into BSSGP_BVC_BLOCK to SGSN\n",
632 nsvc->nsei, nsvc->nsvci);
633 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei,
634 peer->bvci, 0);
635 break;
636 }
637 } else {
638 /* iterate over all BTS peers and send the respective PDU */
639 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
640 switch (signal) {
641 case S_NS_RESET:
642 gprs_ns_tx_reset(peer->nsvc, nssd->cause);
643 break;
644 case S_NS_BLOCK:
645 gprs_ns_tx_block(peer->nsvc, nssd->cause);
646 break;
647 case S_NS_UNBLOCK:
648 gprs_ns_tx_unblock(peer->nsvc);
649 break;
650 }
651 }
652 }
653 return 0;
654}
655
656
657#include <osmocom/vty/command.h>
658
659gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy",
660 SHOW_STR "Display information about the Gb proxy")
661{
662 struct gbprox_peer *peer;
663
664 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
665 struct gprs_nsvc *nsvc = peer->nsvc;
666 struct gprs_ra_id raid;
667 gsm48_parse_ra(&raid, peer->ra);
668
669 vty_out(vty, "NSEI %5u, NS-VC %5u, PTP-BVCI %5u, "
670 "RAC %u-%u-%u-%u",
671 nsvc->nsei, nsvc->nsvci, peer->bvci,
672 raid.mcc, raid.mnc, raid.lac, raid.rac);
673 if (nsvc->ll == GPRS_NS_LL_UDP || nsvc->ll == GPRS_NS_LL_FR_GRE)
674 vty_out(vty, " %s:%u",
675 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
676 ntohs(nsvc->ip.bts_addr.sin_port));
677 if (peer->blocked)
678 vty_out(vty, " [BVC-BLOCKED]");
679
680 vty_out(vty, "%s", VTY_NEWLINE);
681 }
682 return CMD_SUCCESS;
683}