blob: a95929b91389c4beb1d69a0aa2682dd184e52740 [file] [log] [blame]
Harald Welted1991e72010-04-30 20:26:32 +02001/* 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 General Public License as published by
9 * the Free Software Foundation; either version 2 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 General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
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>
31#include <sys/types.h>
Harald Weltef2fdefe2010-05-11 10:15:42 +020032#include <arpa/inet.h>
Harald Welted1991e72010-04-30 20:26:32 +020033
34#include <osmocore/talloc.h>
35#include <osmocore/select.h>
36
37#include <openbsc/signal.h>
38#include <openbsc/debug.h>
39#include <openbsc/gprs_ns.h>
40#include <openbsc/gprs_bssgp.h>
Harald Weltef01709d2010-05-03 18:54:58 +020041#include <openbsc/gb_proxy.h>
Harald Welted1991e72010-04-30 20:26:32 +020042
43struct gbprox_peer {
44 struct llist_head list;
45
46 /* NS-VC over which we send/receive data to this BVC */
47 struct gprs_nsvc *nsvc;
48
49 /* BVCI used for Point-to-Point to this peer */
50 uint16_t bvci;
51
52 /* Routeing Area that this peer is part of (raw 04.08 encoding) */
53 uint8_t ra[6];
54};
55
56/* Linked list of all Gb peers (except SGSN) */
57static LLIST_HEAD(gbprox_bts_peers);
58
Harald Welted1991e72010-04-30 20:26:32 +020059/* Find the gbprox_peer by its BVCI */
60static struct gbprox_peer *peer_by_bvci(uint16_t bvci)
61{
62 struct gbprox_peer *peer;
63 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
64 if (peer->bvci == bvci)
65 return peer;
66 }
67 return NULL;
68}
69
70static struct gbprox_peer *peer_by_nsvc(struct gprs_nsvc *nsvc)
71{
72 struct gbprox_peer *peer;
73 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
74 if (peer->nsvc == nsvc)
75 return peer;
76 }
77 return NULL;
78}
79
80/* look-up a peer by its Routeing Area Code (RAC) */
Harald Weltee49c8292010-05-01 12:10:57 +020081static struct gbprox_peer *peer_by_rac(const uint8_t *ra)
Harald Welted1991e72010-04-30 20:26:32 +020082{
83 struct gbprox_peer *peer;
84 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Harald Welte8ba9d2d2010-05-12 00:07:29 +020085 if (!memcmp(peer->ra, ra, 6))
Harald Welted1991e72010-04-30 20:26:32 +020086 return peer;
87 }
88 return NULL;
89}
90
91/* look-up a peer by its Location Area Code (LAC) */
Harald Weltee49c8292010-05-01 12:10:57 +020092static struct gbprox_peer *peer_by_lac(const uint8_t *la)
Harald Welted1991e72010-04-30 20:26:32 +020093{
94 struct gbprox_peer *peer;
95 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
Harald Welte8ba9d2d2010-05-12 00:07:29 +020096 if (!memcmp(peer->ra, la, 5))
Harald Welted1991e72010-04-30 20:26:32 +020097 return peer;
98 }
99 return NULL;
100}
101
102static struct gbprox_peer *peer_alloc(uint16_t bvci)
103{
104 struct gbprox_peer *peer;
105
106 peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer);
107 if (!peer)
108 return NULL;
109
110 peer->bvci = bvci;
111 llist_add(&peer->list, &gbprox_bts_peers);
112
113 return peer;
114}
115
116static void peer_free(struct gbprox_peer *peer)
117{
118 llist_del(&peer->list);
119 talloc_free(peer);
120}
121
Harald Welte3fb22d62010-05-12 18:10:25 +0000122/* FIXME: this needs to go to libosmocore/msgb.c */
123static struct msgb *msgb_copy(const struct msgb *msg, const char *name)
124{
Holger Hans Peter Freyther337f4d52010-06-08 18:14:37 +0800125 struct openbsc_msgb_cb *old_cb, *new_cb;
Harald Welte3fb22d62010-05-12 18:10:25 +0000126 struct msgb *new_msg;
127
Holger Hans Peter Freyther337f4d52010-06-08 18:14:37 +0800128 new_msg = msgb_alloc(msg->data_len, name);
Harald Welte3fb22d62010-05-12 18:10:25 +0000129 if (!new_msg)
130 return NULL;
131
Harald Welte3fb22d62010-05-12 18:10:25 +0000132 /* copy data */
Holger Hans Peter Freyther337f4d52010-06-08 18:14:37 +0800133 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
134
135 /* copy header */
136 new_msg->len = msg->len;
137 new_msg->data += msg->data - msg->_data;
138 new_msg->head += msg->head - msg->_data;
139 new_msg->tail += msg->tail - msg->_data;
140
141 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
142 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
143 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
144 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
145
146 /* copy GB specific data */
147 old_cb = OBSC_MSGB_CB(msg);
148 new_cb = OBSC_MSGB_CB(new_msg);
149
150 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
151 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
152
153 new_cb->bssgp_cell_id = old_cb->bssgp_cell_id;
154 new_cb->nsei = old_cb->nsei;
155 new_cb->bvci = old_cb->bvci;
156 new_cb->tlli = old_cb->tlli;
Harald Welte3fb22d62010-05-12 18:10:25 +0000157
158 return new_msg;
159}
160
Harald Welte3ad32432010-05-03 19:05:10 +0200161/* strip off the NS header */
162static void strip_ns_hdr(struct msgb *msg)
163{
164 int strip_len = msgb_bssgph(msg) - msg->data;
165 msgb_pull(msg, strip_len);
166}
167
Harald Welted1991e72010-04-30 20:26:32 +0200168/* feed a message down the NS-VC associated with the specified peer */
Harald Welte3fb22d62010-05-12 18:10:25 +0000169static int gbprox_relay2sgsn(struct msgb *old_msg, uint16_t ns_bvci)
Harald Weltef01709d2010-05-03 18:54:58 +0200170{
Harald Welte3fb22d62010-05-12 18:10:25 +0000171 /* create a copy of the message so the old one can
172 * be free()d safely when we return from gbprox_rcvmsg() */
173 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn");
174
Harald Welteb8da0612010-05-11 20:20:13 +0200175 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Harald Weltee661b272010-05-03 19:28:05 +0200176 msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200177
Harald Weltef01709d2010-05-03 18:54:58 +0200178 msgb_bvci(msg) = ns_bvci;
179 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
180
Harald Welte3ad32432010-05-03 19:05:10 +0200181 strip_ns_hdr(msg);
182
Harald Welte874acec2010-05-11 10:01:17 +0200183 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Weltef01709d2010-05-03 18:54:58 +0200184}
185
Harald Weltef01709d2010-05-03 18:54:58 +0200186/* feed a message down the NS-VC associated with the specified peer */
Harald Welte3fb22d62010-05-12 18:10:25 +0000187static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer,
Harald Welted1991e72010-04-30 20:26:32 +0200188 uint16_t ns_bvci)
189{
Harald Welte3fb22d62010-05-12 18:10:25 +0000190 /* create a copy of the message so the old one can
191 * be free()d safely when we return from gbprox_rcvmsg() */
192 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer");
193
Harald Welte0cc900d2010-05-13 10:34:56 +0200194 DEBUGP(DGPRS, "NSEI=%u proxying SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Harald Weltee661b272010-05-03 19:28:05 +0200195 msgb_nsei(msg), ns_bvci, peer->nsvc->nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200196
Harald Welted1991e72010-04-30 20:26:32 +0200197 msgb_bvci(msg) = ns_bvci;
198 msgb_nsei(msg) = peer->nsvc->nsei;
199
Harald Welte0cc900d2010-05-13 10:34:56 +0200200 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte3ad32432010-05-03 19:05:10 +0200201 strip_ns_hdr(msg);
202
Harald Welte874acec2010-05-11 10:01:17 +0200203 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200204}
205
206/* Send a message to a peer identified by ptp_bvci but using ns_bvci
207 * in the NS hdr */
Harald Welte3ad32432010-05-03 19:05:10 +0200208static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
Harald Welted1991e72010-04-30 20:26:32 +0200209 uint16_t ns_bvci)
210{
211 struct gbprox_peer *peer;
212
213 peer = peer_by_bvci(ptp_bvci);
Harald Welte0358aae2010-05-03 21:37:11 +0200214 if (!peer) {
Harald Welte93048cf2010-05-13 14:14:56 +0200215 LOGP(DGPRS, LOGL_ERROR, "BVCI=%u: Cannot find BSS\n",
Harald Welte0358aae2010-05-03 21:37:11 +0200216 ptp_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200217 return -ENOENT;
Harald Welte0358aae2010-05-03 21:37:11 +0200218 }
Harald Welted1991e72010-04-30 20:26:32 +0200219
Harald Welte3ad32432010-05-03 19:05:10 +0200220 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200221}
222
223/* Receive an incoming signalling message from a BSS-side NS-VC */
224static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc,
225 uint16_t ns_bvci)
226{
Harald Welte2ac2e912010-05-03 16:30:59 +0200227 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200228 struct tlv_parsed tp;
229 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200230 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +0200231 struct gbprox_peer *from_peer;
Harald Weltee49c8292010-05-01 12:10:57 +0200232 struct gprs_ra_id raid;
Harald Welted1991e72010-04-30 20:26:32 +0200233
234 if (ns_bvci != 0) {
Harald Welte93048cf2010-05-13 14:14:56 +0200235 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI=%u is not signalling\n",
Harald Welte6aa24d12010-05-03 19:22:32 +0200236 nsvc->nsei, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200237 return -EINVAL;
238 }
239
240 /* we actually should never see those two for BVCI == 0, but double-check
241 * just to make sure */
242 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
243 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte6aa24d12010-05-03 19:22:32 +0200244 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
245 "signalling\n", nsvc->nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200246 return -EINVAL;
247 }
248
249 bssgp_tlv_parse(&tp, bgph->data, data_len);
250
251 switch (pdu_type) {
252 case BSSGP_PDUT_SUSPEND:
253 case BSSGP_PDUT_RESUME:
Harald Weltee49c8292010-05-01 12:10:57 +0200254 /* We implement RAC snooping during SUSPEND/RESUME, since
255 * it establishes a relationsip between BVCI/peer and the
256 * routeing area code. The snooped information is then
257 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
258 * the correct BSSGP */
Harald Welted1991e72010-04-30 20:26:32 +0200259 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
260 goto err_mand_ie;
261 from_peer = peer_by_nsvc(nsvc);
262 if (!from_peer)
263 goto err_no_peer;
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200264 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
265 sizeof(from_peer->ra));
Harald Weltef2fdefe2010-05-11 10:15:42 +0200266 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte93048cf2010-05-13 14:14:56 +0200267 LOGP(DGPRS, LOGL_INFO, "NSEI=%u BSSGP SUSPEND/RESUME "
268 "RAC snooping: RAC %u-%u-%u-%u behind BVCI=%u, "
269 "NSVCI=%u\n",nsvc->nsei, raid.mcc, raid.mnc, raid.lac,
270 raid.rac , from_peer->bvci, nsvc->nsvci);
Harald Welted1991e72010-04-30 20:26:32 +0200271 /* FIXME: This only supports one BSS per RA */
272 break;
Harald Welte6aa24d12010-05-03 19:22:32 +0200273 case BSSGP_PDUT_BVC_RESET:
274 /* If we receive a BVC reset on the signalling endpoint, we
275 * don't want the SGSN to reset, as the signalling endpoint
276 * is common for all point-to-point BVCs (and thus all BTS) */
277 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
278 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Weltebe6e25a2010-05-12 00:20:41 +0200279 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Harald Welteb8da0612010-05-11 20:20:13 +0200280 nsvc->nsei, bvci);
Harald Welte6aa24d12010-05-03 19:22:32 +0200281 if (bvci == 0) {
282 /* FIXME: only do this if SGSN is alive! */
Harald Welteb8da0612010-05-11 20:20:13 +0200283 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Harald Welte6aa24d12010-05-03 19:22:32 +0200284 "BVC RESET ACK of BVCI=0\n", nsvc->nsei);
285 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
286 nsvc->nsei, 0, ns_bvci);
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200287 }
288 from_peer = peer_by_bvci(bvci);
289 if (!from_peer) {
Harald Welte0358aae2010-05-03 21:37:11 +0200290 /* if a PTP-BVC is reset, and we don't know that
291 * PTP-BVCI yet, we should allocate a new peer */
292 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
293 "BVCI=%u via NSVCI=%u/NSEI=%u\n", bvci,
294 nsvc->nsvci, nsvc->nsei);
295 from_peer = peer_alloc(bvci);
296 from_peer->nsvc = nsvc;
Harald Welte6aa24d12010-05-03 19:22:32 +0200297 }
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200298 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
299 struct gprs_ra_id raid;
300 /* We have a Cell Identifier present in this
301 * PDU, this means we can extend our local
302 * state information about this particular cell
303 * */
304 memcpy(from_peer->ra,
305 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
306 sizeof(from_peer->ra));
307 gsm48_parse_ra(&raid, from_peer->ra);
308 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Harald Weltebe6e25a2010-05-12 00:20:41 +0200309 "Cell ID %u-%u-%u-%u\n", nsvc->nsei,
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200310 bvci, raid.mcc, raid.mnc, raid.lac,
311 raid.rac);
312 }
Harald Welte6aa24d12010-05-03 19:22:32 +0200313 }
314 break;
Harald Welted1991e72010-04-30 20:26:32 +0200315 }
316
Harald Welteb8da0612010-05-11 20:20:13 +0200317 /* Normally, we can simply pass on all signalling messages from BSS to
318 * SGSN */
Harald Welte3ad32432010-05-03 19:05:10 +0200319 return gbprox_relay2sgsn(msg, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200320err_no_peer:
Harald Welte874acec2010-05-11 10:01:17 +0200321 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on RAC\n",
322 nsvc->nsei);
323 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200324err_mand_ie:
Harald Welte874acec2010-05-11 10:01:17 +0200325 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
326 nsvc->nsei);
327 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200328}
329
330/* Receive paging request from SGSN, we need to relay to proper BSS */
331static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
332 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
333{
Harald Welte93048cf2010-05-13 14:14:56 +0200334 struct gbprox_peer *peer = NULL;
Harald Welted1991e72010-04-30 20:26:32 +0200335
Harald Welte93048cf2010-05-13 14:14:56 +0200336 LOGP(DGPRS, LOGL_INFO, "NSEI=%u(SGSN) BSSGP PAGING ",
337 nsvc->nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200338 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
339 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte93048cf2010-05-13 14:14:56 +0200340 LOGPC(DGPRS, LOGL_INFO, "routing by BVCI to peer BVCI=%u\n",
341 bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200342 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
343 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte93048cf2010-05-13 14:14:56 +0200344 LOGPC(DGPRS, LOGL_INFO, "routing by RAC to peer BVCI=%u\n",
Holger Hans Peter Freyther98b82c12010-06-08 16:30:24 +0800345 peer ? peer->bvci : -1);
Harald Welted1991e72010-04-30 20:26:32 +0200346 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
347 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Harald Welte93048cf2010-05-13 14:14:56 +0200348 LOGPC(DGPRS, LOGL_INFO, "routing by LAC to peer BVCI=%u\n",
Holger Hans Peter Freyther98b82c12010-06-08 16:30:24 +0800349 peer ? peer->bvci : -1);
Harald Welted1991e72010-04-30 20:26:32 +0200350 } else
Harald Welte93048cf2010-05-13 14:14:56 +0200351 LOGPC(DGPRS, LOGL_INFO, "\n");
352
353 if (!peer) {
354 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) BSSGP PAGING: "
355 "unable to route, missing IE\n", nsvc->nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200356 return -EINVAL;
Harald Welte93048cf2010-05-13 14:14:56 +0200357 }
358 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200359}
360
Harald Welte874acec2010-05-11 10:01:17 +0200361/* Receive an incoming BVC-RESET message from the SGSN */
362static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp,
363 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
364{
365 struct gbprox_peer *peer;
366 uint16_t ptp_bvci;
367
368 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
369 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
370 NULL, msg);
371 }
372 ptp_bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
373
374 if (ptp_bvci >= 2) {
375 /* A reset for a PTP BVC was received, forward it to its
376 * respective peer */
377 peer = peer_by_bvci(ptp_bvci);
378 if (!peer) {
Harald Welte93048cf2010-05-13 14:14:56 +0200379 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u BVCI=%u: Cannot find BSS\n",
380 nsvc->nsei, ptp_bvci);
Harald Welte874acec2010-05-11 10:01:17 +0200381 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
382 NULL, msg);
383 }
384 return gbprox_relay2peer(msg, peer, ns_bvci);
385 }
386
387 /* A reset for the Signalling entity has been received
388 * from the SGSN. As the signalling BVCI is shared
389 * among all the BSS's that we multiplex, it needs to
390 * be relayed */
391 llist_for_each_entry(peer, &gbprox_bts_peers, list)
392 gbprox_relay2peer(msg, peer, ns_bvci);
393
394 return 0;
395}
396
Harald Welted1991e72010-04-30 20:26:32 +0200397/* Receive an incoming signalling message from the SGSN-side NS-VC */
398static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc,
399 uint16_t ns_bvci)
400{
Harald Welte2ac2e912010-05-03 16:30:59 +0200401 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200402 struct tlv_parsed tp;
403 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200404 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +0200405 struct gbprox_peer *peer;
406 uint16_t bvci;
407 int rc = 0;
408
409 if (ns_bvci != 0) {
Harald Welte93048cf2010-05-13 14:14:56 +0200410 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI=%u is not "
Harald Welte6aa24d12010-05-03 19:22:32 +0200411 "signalling\n", nsvc->nsei, ns_bvci);
Harald Welte874acec2010-05-11 10:01:17 +0200412 /* FIXME: Send proper error message */
Harald Welted1991e72010-04-30 20:26:32 +0200413 return -EINVAL;
414 }
415
416 /* we actually should never see those two for BVCI == 0, but double-check
417 * just to make sure */
418 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
419 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte6aa24d12010-05-03 19:22:32 +0200420 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
421 "signalling\n", nsvc->nsei);
Harald Welte874acec2010-05-11 10:01:17 +0200422 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200423 }
424
425 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
426
427 switch (pdu_type) {
Harald Welte874acec2010-05-11 10:01:17 +0200428 case BSSGP_PDUT_BVC_RESET:
429 rc = rx_reset_from_sgsn(msg, &tp, nsvc, ns_bvci);
430 break;
Harald Welted1991e72010-04-30 20:26:32 +0200431 case BSSGP_PDUT_FLUSH_LL:
432 case BSSGP_PDUT_BVC_BLOCK_ACK:
433 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welted1991e72010-04-30 20:26:32 +0200434 case BSSGP_PDUT_BVC_RESET_ACK:
435 /* simple case: BVCI IE is mandatory */
436 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
437 goto err_mand_ie;
438 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welte3ad32432010-05-03 19:05:10 +0200439 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200440 break;
441 case BSSGP_PDUT_PAGING_PS:
442 case BSSGP_PDUT_PAGING_CS:
443 /* process the paging request (LAC/RAC lookup) */
444 rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci);
445 break;
446 case BSSGP_PDUT_STATUS:
Harald Welte874acec2010-05-11 10:01:17 +0200447 /* Some exception has occurred */
Harald Welte6aa24d12010-05-03 19:22:32 +0200448 LOGP(DGPRS, LOGL_NOTICE,
Harald Welte93048cf2010-05-13 14:14:56 +0200449 "NSEI=%u(SGSN) BSSGP STATUS ", nsvc->nsei);
Harald Welte874acec2010-05-11 10:01:17 +0200450 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
451 LOGPC(DGPRS, LOGL_NOTICE, "\n");
452 goto err_mand_ie;
453 }
454 LOGPC(DGPRS, LOGL_NOTICE,
455 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
456 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
457 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Harald Weltef2fdefe2010-05-11 10:15:42 +0200458 uint16_t *bvci = (uint16_t *)
459 TLVP_VAL(&tp, BSSGP_IE_BVCI);
Harald Welte874acec2010-05-11 10:01:17 +0200460 LOGPC(DGPRS, LOGL_NOTICE,
461 "BVCI=%u\n", ntohs(*bvci));
462 } else
463 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welted1991e72010-04-30 20:26:32 +0200464 break;
465 /* those only exist in the SGSN -> BSS direction */
466 case BSSGP_PDUT_SUSPEND_ACK:
467 case BSSGP_PDUT_SUSPEND_NACK:
468 case BSSGP_PDUT_RESUME_ACK:
469 case BSSGP_PDUT_RESUME_NACK:
470 /* RAC IE is mandatory */
471 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
472 goto err_mand_ie;
473 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
474 if (!peer)
475 goto err_no_peer;
Harald Welte3ad32432010-05-03 19:05:10 +0200476 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200477 break;
478 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte6aa24d12010-05-03 19:22:32 +0200479 LOGP(DGPRS, LOGL_ERROR,
Harald Welte93048cf2010-05-13 14:14:56 +0200480 "NSEI=%u(SGSN) BSSGP INVOKE TRACE not supported\n",nsvc->nsei);
Harald Welte874acec2010-05-11 10:01:17 +0200481 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200482 break;
483 default:
Harald Weltebe6e25a2010-05-12 00:20:41 +0200484 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
485 pdu_type);
Harald Welte874acec2010-05-11 10:01:17 +0200486 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200487 break;
488 }
489
490 return rc;
491err_mand_ie:
Harald Welte0358aae2010-05-03 21:37:11 +0200492 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
493 nsvc->nsei);
Harald Welte874acec2010-05-11 10:01:17 +0200494 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200495err_no_peer:
Harald Welte874acec2010-05-11 10:01:17 +0200496 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n",
497 nsvc->nsei);
498 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200499}
500
501/* Main input function for Gb proxy */
502int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci)
503{
Harald Weltef01709d2010-05-03 18:54:58 +0200504 int rc;
Harald Welted1991e72010-04-30 20:26:32 +0200505
506 /* Only BVCI=0 messages need special treatment */
507 if (ns_bvci == 0 || ns_bvci == 1) {
508 if (nsvc->remote_end_is_sgsn)
Harald Weltef01709d2010-05-03 18:54:58 +0200509 rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200510 else
Harald Weltef01709d2010-05-03 18:54:58 +0200511 rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
512 } else {
513 /* All other BVCI are PTP and thus can be simply forwarded */
Harald Welte0358aae2010-05-03 21:37:11 +0200514 if (!nsvc->remote_end_is_sgsn) {
Harald Welte3ad32432010-05-03 19:05:10 +0200515 rc = gbprox_relay2sgsn(msg, ns_bvci);
Harald Weltee49c8292010-05-01 12:10:57 +0200516 } else {
Harald Weltef01709d2010-05-03 18:54:58 +0200517 struct gbprox_peer *peer = peer_by_bvci(ns_bvci);
518 if (!peer) {
Harald Weltebe6e25a2010-05-12 00:20:41 +0200519 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Harald Weltef01709d2010-05-03 18:54:58 +0200520 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
521 nsvc->nsvci, nsvc->nsei);
522 peer = peer_alloc(ns_bvci);
523 peer->nsvc = nsvc;
524 }
Harald Welte3ad32432010-05-03 19:05:10 +0200525 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Weltee49c8292010-05-01 12:10:57 +0200526 }
Harald Welted1991e72010-04-30 20:26:32 +0200527 }
528
Harald Weltef01709d2010-05-03 18:54:58 +0200529 return rc;
Harald Welted1991e72010-04-30 20:26:32 +0200530}
Harald Welteb34e2c12010-05-11 05:49:43 +0200531
Harald Welte9ee404a2010-05-14 11:53:08 +0000532int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi)
533{
534 struct gprs_nsvc *nsvc;
535
536 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
537 if (!nsvc->persistent)
538 continue;
539 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
540 }
541 return 0;
542}
543
Harald Welte5bb0d362010-05-11 06:34:24 +0200544/* Signal handler for signals from NS layer */
545int gbprox_signal(unsigned int subsys, unsigned int signal,
546 void *handler_data, void *signal_data)
547{
548 struct ns_signal_data *nssd = signal_data;
549 struct gprs_nsvc *nsvc = nssd->nsvc;
550 struct gbprox_peer *peer;
551
552 if (subsys != SS_NS)
553 return 0;
554
Harald Welte15cbb0e2010-05-11 18:29:44 +0200555 if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) {
556 /* We have received a NS-RESET from the NSEI and NSVC
557 * of the SGSN. This might happen with SGSN that start
558 * their own NS-RESET procedure without waiting for our
559 * NS-RESET */
560 nsvc->remote_end_is_sgsn = 1;
561 }
562
Harald Weltefa41cb72010-05-12 13:28:25 +0000563 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
564 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
565 "re-starting RESET procedure\n");
566 nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, nsvc->nsei,
567 nsvc->nsvci);
568 }
569
Harald Welte5bb0d362010-05-11 06:34:24 +0200570 /* We currently only care about signals from the SGSN */
571 if (!nsvc->remote_end_is_sgsn)
572 return 0;
573
574 /* iterate over all BTS peers and send the respective PDU */
575 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
576 switch (signal) {
577 case S_NS_RESET:
578 gprs_ns_tx_reset(peer->nsvc, nssd->cause);
579 break;
580 case S_NS_BLOCK:
581 gprs_ns_tx_block(peer->nsvc, nssd->cause);
582 break;
583 case S_NS_UNBLOCK:
584 gprs_ns_tx_unblock(peer->nsvc);
585 break;
586 }
587 }
588 return 0;
589}
590
Harald Welteb34e2c12010-05-11 05:49:43 +0200591
Harald Weltebd9591f2010-05-19 19:45:32 +0200592#include <osmocom/vty/command.h>
Harald Welteb34e2c12010-05-11 05:49:43 +0200593
594gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy",
595 SHOW_STR "Display information about the Gb proxy")
596{
597 struct gbprox_peer *peer;
598
599 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
600 struct gprs_nsvc *nsvc = peer->nsvc;
601 struct gprs_ra_id raid;
Harald Weltef2fdefe2010-05-11 10:15:42 +0200602 gsm48_parse_ra(&raid, peer->ra);
Harald Welteb34e2c12010-05-11 05:49:43 +0200603
Harald Welte4f0675b2010-05-19 17:42:20 +0200604 vty_out(vty, "NSEI %5u, NS-VC %5u, PTP-BVCI %5u, "
Harald Welte6265d372010-05-19 17:06:16 +0200605 "RAC %u-%u-%u-%u",
Harald Welteb34e2c12010-05-11 05:49:43 +0200606 nsvc->nsei, nsvc->nsvci, peer->bvci,
Harald Welte6265d372010-05-19 17:06:16 +0200607 raid.mcc, raid.mnc, raid.lac, raid.rac);
608 if (nsvc->ll == GPRS_NS_LL_UDP || nsvc->ll == GPRS_NS_LL_FR_GRE)
609 vty_out(vty, " %s:%u%s",
Harald Welteb34e2c12010-05-11 05:49:43 +0200610 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
611 ntohs(nsvc->ip.bts_addr.sin_port), VTY_NEWLINE);
Harald Welte6265d372010-05-19 17:06:16 +0200612 else
613 vty_out(vty, "%s", VTY_NEWLINE);
Harald Welteb34e2c12010-05-11 05:49:43 +0200614 }
615 return CMD_SUCCESS;
616}