blob: 7203a4bc28657b02e3eb2135683c2fbe7d84aed7 [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{
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 header */
132 memcpy(new_msg, msg, sizeof(*new_msg));
133 /* copy data */
134 memcpy(new_msg->data, msg->data, new_msg->data_len);
135
136 return new_msg;
137}
138
Harald Welte3ad32432010-05-03 19:05:10 +0200139/* strip off the NS header */
140static void strip_ns_hdr(struct msgb *msg)
141{
142 int strip_len = msgb_bssgph(msg) - msg->data;
143 msgb_pull(msg, strip_len);
144}
145
Harald Welted1991e72010-04-30 20:26:32 +0200146/* feed a message down the NS-VC associated with the specified peer */
Harald Welte3fb22d62010-05-12 18:10:25 +0000147static int gbprox_relay2sgsn(struct msgb *old_msg, uint16_t ns_bvci)
Harald Weltef01709d2010-05-03 18:54:58 +0200148{
Harald Welte3fb22d62010-05-12 18:10:25 +0000149 /* create a copy of the message so the old one can
150 * be free()d safely when we return from gbprox_rcvmsg() */
151 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2sgsn");
152
Harald Welteb8da0612010-05-11 20:20:13 +0200153 DEBUGP(DGPRS, "NSEI=%u proxying BTS->SGSN (NS_BVCI=%u, NSEI=%u)\n",
Harald Weltee661b272010-05-03 19:28:05 +0200154 msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200155
Harald Weltef01709d2010-05-03 18:54:58 +0200156 msgb_bvci(msg) = ns_bvci;
157 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
158
Harald Welte3ad32432010-05-03 19:05:10 +0200159 strip_ns_hdr(msg);
160
Harald Welte874acec2010-05-11 10:01:17 +0200161 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Weltef01709d2010-05-03 18:54:58 +0200162}
163
Harald Weltef01709d2010-05-03 18:54:58 +0200164/* feed a message down the NS-VC associated with the specified peer */
Harald Welte3fb22d62010-05-12 18:10:25 +0000165static int gbprox_relay2peer(struct msgb *old_msg, struct gbprox_peer *peer,
Harald Welted1991e72010-04-30 20:26:32 +0200166 uint16_t ns_bvci)
167{
Harald Welte3fb22d62010-05-12 18:10:25 +0000168 /* create a copy of the message so the old one can
169 * be free()d safely when we return from gbprox_rcvmsg() */
170 struct msgb *msg = msgb_copy(old_msg, "msgb_relay2peer");
171
Harald Welteb8da0612010-05-11 20:20:13 +0200172 DEBUGP(DGPRS, "NSEI=%u proxying to SGSN->BSS (NS_BVCI=%u, NSEI=%u)\n",
Harald Weltee661b272010-05-03 19:28:05 +0200173 msgb_nsei(msg), ns_bvci, peer->nsvc->nsei);
Harald Welte6aa24d12010-05-03 19:22:32 +0200174
Harald Welted1991e72010-04-30 20:26:32 +0200175 msgb_bvci(msg) = ns_bvci;
176 msgb_nsei(msg) = peer->nsvc->nsei;
177
Harald Welte3ad32432010-05-03 19:05:10 +0200178 strip_ns_hdr(msg);
179
Harald Welte874acec2010-05-11 10:01:17 +0200180 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200181}
182
183/* Send a message to a peer identified by ptp_bvci but using ns_bvci
184 * in the NS hdr */
Harald Welte3ad32432010-05-03 19:05:10 +0200185static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
Harald Welted1991e72010-04-30 20:26:32 +0200186 uint16_t ns_bvci)
187{
188 struct gbprox_peer *peer;
189
190 peer = peer_by_bvci(ptp_bvci);
Harald Welte0358aae2010-05-03 21:37:11 +0200191 if (!peer) {
192 LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n",
193 ptp_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200194 return -ENOENT;
Harald Welte0358aae2010-05-03 21:37:11 +0200195 }
Harald Welted1991e72010-04-30 20:26:32 +0200196
Harald Welte3ad32432010-05-03 19:05:10 +0200197 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200198}
199
200/* Receive an incoming signalling message from a BSS-side NS-VC */
201static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc,
202 uint16_t ns_bvci)
203{
Harald Welte2ac2e912010-05-03 16:30:59 +0200204 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200205 struct tlv_parsed tp;
206 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200207 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +0200208 struct gbprox_peer *from_peer;
Harald Weltee49c8292010-05-01 12:10:57 +0200209 struct gprs_ra_id raid;
Harald Welted1991e72010-04-30 20:26:32 +0200210
211 if (ns_bvci != 0) {
Harald Welte6aa24d12010-05-03 19:22:32 +0200212 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI %u is not signalling\n",
213 nsvc->nsei, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200214 return -EINVAL;
215 }
216
217 /* we actually should never see those two for BVCI == 0, but double-check
218 * just to make sure */
219 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
220 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte6aa24d12010-05-03 19:22:32 +0200221 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
222 "signalling\n", nsvc->nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200223 return -EINVAL;
224 }
225
226 bssgp_tlv_parse(&tp, bgph->data, data_len);
227
228 switch (pdu_type) {
229 case BSSGP_PDUT_SUSPEND:
230 case BSSGP_PDUT_RESUME:
Harald Weltee49c8292010-05-01 12:10:57 +0200231 /* We implement RAC snooping during SUSPEND/RESUME, since
232 * it establishes a relationsip between BVCI/peer and the
233 * routeing area code. The snooped information is then
234 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
235 * the correct BSSGP */
Harald Welted1991e72010-04-30 20:26:32 +0200236 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
237 goto err_mand_ie;
238 from_peer = peer_by_nsvc(nsvc);
239 if (!from_peer)
240 goto err_no_peer;
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200241 memcpy(from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
242 sizeof(from_peer->ra));
Harald Weltef2fdefe2010-05-11 10:15:42 +0200243 gsm48_parse_ra(&raid, from_peer->ra);
Harald Weltebe6e25a2010-05-12 00:20:41 +0200244 LOGP(DGPRS, LOGL_INFO, "NSEI=%u RAC snooping: RAC %u-%u-%u-%u "
245 "behind BVCI=%u, NSVCI=%u\n", nsvc->nsei, raid.mcc,
246 raid.mnc, raid.lac, raid.rac , from_peer->bvci,
247 nsvc->nsvci);
Harald Welted1991e72010-04-30 20:26:32 +0200248 /* FIXME: This only supports one BSS per RA */
249 break;
Harald Welte6aa24d12010-05-03 19:22:32 +0200250 case BSSGP_PDUT_BVC_RESET:
251 /* If we receive a BVC reset on the signalling endpoint, we
252 * don't want the SGSN to reset, as the signalling endpoint
253 * is common for all point-to-point BVCs (and thus all BTS) */
254 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
255 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Weltebe6e25a2010-05-12 00:20:41 +0200256 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Rx BVC RESET (BVCI=%u)\n",
Harald Welteb8da0612010-05-11 20:20:13 +0200257 nsvc->nsei, bvci);
Harald Welte6aa24d12010-05-03 19:22:32 +0200258 if (bvci == 0) {
259 /* FIXME: only do this if SGSN is alive! */
Harald Welteb8da0612010-05-11 20:20:13 +0200260 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Tx fake "
Harald Welte6aa24d12010-05-03 19:22:32 +0200261 "BVC RESET ACK of BVCI=0\n", nsvc->nsei);
262 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
263 nsvc->nsei, 0, ns_bvci);
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200264 }
265 from_peer = peer_by_bvci(bvci);
266 if (!from_peer) {
Harald Welte0358aae2010-05-03 21:37:11 +0200267 /* if a PTP-BVC is reset, and we don't know that
268 * PTP-BVCI yet, we should allocate a new peer */
269 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
270 "BVCI=%u via NSVCI=%u/NSEI=%u\n", bvci,
271 nsvc->nsvci, nsvc->nsei);
272 from_peer = peer_alloc(bvci);
273 from_peer->nsvc = nsvc;
Harald Welte6aa24d12010-05-03 19:22:32 +0200274 }
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200275 if (TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID)) {
276 struct gprs_ra_id raid;
277 /* We have a Cell Identifier present in this
278 * PDU, this means we can extend our local
279 * state information about this particular cell
280 * */
281 memcpy(from_peer->ra,
282 TLVP_VAL(&tp, BSSGP_IE_CELL_ID),
283 sizeof(from_peer->ra));
284 gsm48_parse_ra(&raid, from_peer->ra);
285 LOGP(DGPRS, LOGL_INFO, "NSEI=%u/BVCI=%u "
Harald Weltebe6e25a2010-05-12 00:20:41 +0200286 "Cell ID %u-%u-%u-%u\n", nsvc->nsei,
Harald Welte8ba9d2d2010-05-12 00:07:29 +0200287 bvci, raid.mcc, raid.mnc, raid.lac,
288 raid.rac);
289 }
Harald Welte6aa24d12010-05-03 19:22:32 +0200290 }
291 break;
Harald Welted1991e72010-04-30 20:26:32 +0200292 }
293
Harald Welteb8da0612010-05-11 20:20:13 +0200294 /* Normally, we can simply pass on all signalling messages from BSS to
295 * SGSN */
Harald Welte3ad32432010-05-03 19:05:10 +0200296 return gbprox_relay2sgsn(msg, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200297err_no_peer:
Harald Welte874acec2010-05-11 10:01:17 +0200298 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on RAC\n",
299 nsvc->nsei);
300 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200301err_mand_ie:
Harald Welte874acec2010-05-11 10:01:17 +0200302 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
303 nsvc->nsei);
304 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200305}
306
307/* Receive paging request from SGSN, we need to relay to proper BSS */
308static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
309 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
310{
311 struct gbprox_peer *peer;
312
313 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
314 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte3ad32432010-05-03 19:05:10 +0200315 return gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200316 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
317 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte3ad32432010-05-03 19:05:10 +0200318 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200319 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
320 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Harald Welte3ad32432010-05-03 19:05:10 +0200321 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200322 } else
323 return -EINVAL;
324}
325
Harald Welte874acec2010-05-11 10:01:17 +0200326/* Receive an incoming BVC-RESET message from the SGSN */
327static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp,
328 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
329{
330 struct gbprox_peer *peer;
331 uint16_t ptp_bvci;
332
333 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
334 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
335 NULL, msg);
336 }
337 ptp_bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
338
339 if (ptp_bvci >= 2) {
340 /* A reset for a PTP BVC was received, forward it to its
341 * respective peer */
342 peer = peer_by_bvci(ptp_bvci);
343 if (!peer) {
344 LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n",
345 ptp_bvci);
346 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
347 NULL, msg);
348 }
349 return gbprox_relay2peer(msg, peer, ns_bvci);
350 }
351
352 /* A reset for the Signalling entity has been received
353 * from the SGSN. As the signalling BVCI is shared
354 * among all the BSS's that we multiplex, it needs to
355 * be relayed */
356 llist_for_each_entry(peer, &gbprox_bts_peers, list)
357 gbprox_relay2peer(msg, peer, ns_bvci);
358
359 return 0;
360}
361
Harald Welted1991e72010-04-30 20:26:32 +0200362/* Receive an incoming signalling message from the SGSN-side NS-VC */
363static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc,
364 uint16_t ns_bvci)
365{
Harald Welte2ac2e912010-05-03 16:30:59 +0200366 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200367 struct tlv_parsed tp;
368 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200369 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +0200370 struct gbprox_peer *peer;
371 uint16_t bvci;
372 int rc = 0;
373
374 if (ns_bvci != 0) {
Harald Welte6aa24d12010-05-03 19:22:32 +0200375 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI %u is not "
376 "signalling\n", nsvc->nsei, ns_bvci);
Harald Welte874acec2010-05-11 10:01:17 +0200377 /* FIXME: Send proper error message */
Harald Welted1991e72010-04-30 20:26:32 +0200378 return -EINVAL;
379 }
380
381 /* we actually should never see those two for BVCI == 0, but double-check
382 * just to make sure */
383 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
384 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte6aa24d12010-05-03 19:22:32 +0200385 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
386 "signalling\n", nsvc->nsei);
Harald Welte874acec2010-05-11 10:01:17 +0200387 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200388 }
389
390 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
391
392 switch (pdu_type) {
Harald Welte874acec2010-05-11 10:01:17 +0200393 case BSSGP_PDUT_BVC_RESET:
394 rc = rx_reset_from_sgsn(msg, &tp, nsvc, ns_bvci);
395 break;
Harald Welted1991e72010-04-30 20:26:32 +0200396 case BSSGP_PDUT_FLUSH_LL:
397 case BSSGP_PDUT_BVC_BLOCK_ACK:
398 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welted1991e72010-04-30 20:26:32 +0200399 case BSSGP_PDUT_BVC_RESET_ACK:
400 /* simple case: BVCI IE is mandatory */
401 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
402 goto err_mand_ie;
403 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welte3ad32432010-05-03 19:05:10 +0200404 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200405 break;
406 case BSSGP_PDUT_PAGING_PS:
407 case BSSGP_PDUT_PAGING_CS:
408 /* process the paging request (LAC/RAC lookup) */
409 rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci);
410 break;
411 case BSSGP_PDUT_STATUS:
Harald Welte874acec2010-05-11 10:01:17 +0200412 /* Some exception has occurred */
Harald Welte6aa24d12010-05-03 19:22:32 +0200413 LOGP(DGPRS, LOGL_NOTICE,
Harald Welte874acec2010-05-11 10:01:17 +0200414 "NSEI=%u(SGSN) STATUS ", nsvc->nsei);
415 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
416 LOGPC(DGPRS, LOGL_NOTICE, "\n");
417 goto err_mand_ie;
418 }
419 LOGPC(DGPRS, LOGL_NOTICE,
420 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
421 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
422 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Harald Weltef2fdefe2010-05-11 10:15:42 +0200423 uint16_t *bvci = (uint16_t *)
424 TLVP_VAL(&tp, BSSGP_IE_BVCI);
Harald Welte874acec2010-05-11 10:01:17 +0200425 LOGPC(DGPRS, LOGL_NOTICE,
426 "BVCI=%u\n", ntohs(*bvci));
427 } else
428 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welted1991e72010-04-30 20:26:32 +0200429 break;
430 /* those only exist in the SGSN -> BSS direction */
431 case BSSGP_PDUT_SUSPEND_ACK:
432 case BSSGP_PDUT_SUSPEND_NACK:
433 case BSSGP_PDUT_RESUME_ACK:
434 case BSSGP_PDUT_RESUME_NACK:
435 /* RAC IE is mandatory */
436 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
437 goto err_mand_ie;
438 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
439 if (!peer)
440 goto err_no_peer;
Harald Welte3ad32432010-05-03 19:05:10 +0200441 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200442 break;
443 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte6aa24d12010-05-03 19:22:32 +0200444 LOGP(DGPRS, LOGL_ERROR,
445 "NSEI=%u(SGSN) INVOKE TRACE not supported\n", nsvc->nsei);
Harald Welte874acec2010-05-11 10:01:17 +0200446 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200447 break;
448 default:
Harald Weltebe6e25a2010-05-12 00:20:41 +0200449 LOGP(DGPRS, LOGL_NOTICE, "BSSGP PDU type 0x%02x unknown\n",
450 pdu_type);
Harald Welte874acec2010-05-11 10:01:17 +0200451 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200452 break;
453 }
454
455 return rc;
456err_mand_ie:
Harald Welte0358aae2010-05-03 21:37:11 +0200457 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
458 nsvc->nsei);
Harald Welte874acec2010-05-11 10:01:17 +0200459 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200460err_no_peer:
Harald Welte874acec2010-05-11 10:01:17 +0200461 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n",
462 nsvc->nsei);
463 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welted1991e72010-04-30 20:26:32 +0200464}
465
466/* Main input function for Gb proxy */
467int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci)
468{
Harald Weltef01709d2010-05-03 18:54:58 +0200469 int rc;
Harald Welted1991e72010-04-30 20:26:32 +0200470
471 /* Only BVCI=0 messages need special treatment */
472 if (ns_bvci == 0 || ns_bvci == 1) {
473 if (nsvc->remote_end_is_sgsn)
Harald Weltef01709d2010-05-03 18:54:58 +0200474 rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200475 else
Harald Weltef01709d2010-05-03 18:54:58 +0200476 rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
477 } else {
478 /* All other BVCI are PTP and thus can be simply forwarded */
Harald Welte0358aae2010-05-03 21:37:11 +0200479 if (!nsvc->remote_end_is_sgsn) {
Harald Welte3ad32432010-05-03 19:05:10 +0200480 rc = gbprox_relay2sgsn(msg, ns_bvci);
Harald Weltee49c8292010-05-01 12:10:57 +0200481 } else {
Harald Weltef01709d2010-05-03 18:54:58 +0200482 struct gbprox_peer *peer = peer_by_bvci(ns_bvci);
483 if (!peer) {
Harald Weltebe6e25a2010-05-12 00:20:41 +0200484 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
Harald Weltef01709d2010-05-03 18:54:58 +0200485 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
486 nsvc->nsvci, nsvc->nsei);
487 peer = peer_alloc(ns_bvci);
488 peer->nsvc = nsvc;
489 }
Harald Welte3ad32432010-05-03 19:05:10 +0200490 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Weltee49c8292010-05-01 12:10:57 +0200491 }
Harald Welted1991e72010-04-30 20:26:32 +0200492 }
493
Harald Welte3fb22d62010-05-12 18:10:25 +0000494 /* We free the original message here, as we will have created a
495 * copy in case it is forwarded to another peer */
496 msgb_free(msg);
497
Harald Weltef01709d2010-05-03 18:54:58 +0200498 return rc;
Harald Welted1991e72010-04-30 20:26:32 +0200499}
Harald Welteb34e2c12010-05-11 05:49:43 +0200500
Harald Welte5bb0d362010-05-11 06:34:24 +0200501/* Signal handler for signals from NS layer */
502int gbprox_signal(unsigned int subsys, unsigned int signal,
503 void *handler_data, void *signal_data)
504{
505 struct ns_signal_data *nssd = signal_data;
506 struct gprs_nsvc *nsvc = nssd->nsvc;
507 struct gbprox_peer *peer;
508
509 if (subsys != SS_NS)
510 return 0;
511
Harald Welte15cbb0e2010-05-11 18:29:44 +0200512 if (signal == S_NS_RESET && nsvc->nsei == gbcfg.nsip_sgsn_nsei) {
513 /* We have received a NS-RESET from the NSEI and NSVC
514 * of the SGSN. This might happen with SGSN that start
515 * their own NS-RESET procedure without waiting for our
516 * NS-RESET */
517 nsvc->remote_end_is_sgsn = 1;
518 }
519
Harald Weltefa41cb72010-05-12 13:28:25 +0000520 if (signal == S_NS_ALIVE_EXP && nsvc->remote_end_is_sgsn) {
521 LOGP(DGPRS, LOGL_NOTICE, "Tns alive expired too often, "
522 "re-starting RESET procedure\n");
523 nsip_connect(nsvc->nsi, &nsvc->ip.bts_addr, nsvc->nsei,
524 nsvc->nsvci);
525 }
526
Harald Welte5bb0d362010-05-11 06:34:24 +0200527 /* We currently only care about signals from the SGSN */
528 if (!nsvc->remote_end_is_sgsn)
529 return 0;
530
531 /* iterate over all BTS peers and send the respective PDU */
532 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
533 switch (signal) {
534 case S_NS_RESET:
535 gprs_ns_tx_reset(peer->nsvc, nssd->cause);
536 break;
537 case S_NS_BLOCK:
538 gprs_ns_tx_block(peer->nsvc, nssd->cause);
539 break;
540 case S_NS_UNBLOCK:
541 gprs_ns_tx_unblock(peer->nsvc);
542 break;
543 }
544 }
545 return 0;
546}
547
Harald Welteb34e2c12010-05-11 05:49:43 +0200548
549#include <vty/command.h>
550
551gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy",
552 SHOW_STR "Display information about the Gb proxy")
553{
554 struct gbprox_peer *peer;
555
556 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
557 struct gprs_nsvc *nsvc = peer->nsvc;
558 struct gprs_ra_id raid;
Harald Weltef2fdefe2010-05-11 10:15:42 +0200559 gsm48_parse_ra(&raid, peer->ra);
Harald Welteb34e2c12010-05-11 05:49:43 +0200560
561 vty_out(vty, "NSEI %5u, NS-VC %5u, PTP-BVCI %u, "
562 "RAC %u-%u-%u-%u%s",
563 nsvc->nsei, nsvc->nsvci, peer->bvci,
564 raid.mcc, raid.mnc, raid.lac, raid.rac, VTY_NEWLINE);
565 if (nsvc->nsi->ll == GPRS_NS_LL_UDP)
566 vty_out(vty, " remote address %s:%u%s",
567 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
568 ntohs(nsvc->ip.bts_addr.sin_port), VTY_NEWLINE);
569 }
570 return CMD_SUCCESS;
571}