blob: ff80e92d47f07a440683a085c7f1c07ff08c8b4c [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>
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>
32
33#include <osmocore/talloc.h>
34#include <osmocore/select.h>
35
36#include <openbsc/signal.h>
37#include <openbsc/debug.h>
38#include <openbsc/gprs_ns.h>
39#include <openbsc/gprs_bssgp.h>
Harald Welte672f5c42010-05-03 18:54:58 +020040#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020041
42struct gbprox_peer {
43 struct llist_head list;
44
45 /* NS-VC over which we send/receive data to this BVC */
46 struct gprs_nsvc *nsvc;
47
48 /* BVCI used for Point-to-Point to this peer */
49 uint16_t bvci;
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
Harald Welte9f75c352010-04-30 20:26:32 +020058/* 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) */
Harald Welte70f38d22010-05-01 12:10:57 +020080static struct gbprox_peer *peer_by_rac(const uint8_t *ra)
Harald Welte9f75c352010-04-30 20:26:32 +020081{
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) */
Harald Welte70f38d22010-05-01 12:10:57 +020091static struct gbprox_peer *peer_by_lac(const uint8_t *la)
Harald Welte9f75c352010-04-30 20:26:32 +020092{
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
Harald Welte69619e32010-05-03 19:05:10 +0200121/* strip off the NS header */
122static void strip_ns_hdr(struct msgb *msg)
123{
124 int strip_len = msgb_bssgph(msg) - msg->data;
125 msgb_pull(msg, strip_len);
126}
127
Harald Welte9f75c352010-04-30 20:26:32 +0200128/* feed a message down the NS-VC associated with the specified peer */
Harald Welte69619e32010-05-03 19:05:10 +0200129static int gbprox_relay2sgsn(struct msgb *msg, uint16_t ns_bvci)
Harald Welte672f5c42010-05-03 18:54:58 +0200130{
Harald Welte44c48302010-05-03 19:22:32 +0200131 DEBUGP(DGPRS, "NSEI=%u proxying to SGSN (NS_BVCI=%u, NSEI=%u)\n",
Harald Welte96f71f22010-05-03 19:28:05 +0200132 msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200133
Harald Welte672f5c42010-05-03 18:54:58 +0200134 msgb_bvci(msg) = ns_bvci;
135 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
136
Harald Welte69619e32010-05-03 19:05:10 +0200137 strip_ns_hdr(msg);
138
Harald Welte0a4050c2010-05-11 10:01:17 +0200139 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte672f5c42010-05-03 18:54:58 +0200140}
141
Harald Welte672f5c42010-05-03 18:54:58 +0200142/* feed a message down the NS-VC associated with the specified peer */
Harald Welte69619e32010-05-03 19:05:10 +0200143static int gbprox_relay2peer(struct msgb *msg, struct gbprox_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200144 uint16_t ns_bvci)
145{
Harald Welte1c77c6e2010-05-03 21:37:11 +0200146 DEBUGP(DGPRS, "NSEI=%u proxying to BSS (NS_BVCI=%u, NSEI=%u)\n",
Harald Welte96f71f22010-05-03 19:28:05 +0200147 msgb_nsei(msg), ns_bvci, peer->nsvc->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200148
Harald Welte9f75c352010-04-30 20:26:32 +0200149 msgb_bvci(msg) = ns_bvci;
150 msgb_nsei(msg) = peer->nsvc->nsei;
151
Harald Welte69619e32010-05-03 19:05:10 +0200152 strip_ns_hdr(msg);
153
Harald Welte0a4050c2010-05-11 10:01:17 +0200154 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200155}
156
157/* Send a message to a peer identified by ptp_bvci but using ns_bvci
158 * in the NS hdr */
Harald Welte69619e32010-05-03 19:05:10 +0200159static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200160 uint16_t ns_bvci)
161{
162 struct gbprox_peer *peer;
163
164 peer = peer_by_bvci(ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200165 if (!peer) {
166 LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n",
167 ptp_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200168 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200169 }
Harald Welte9f75c352010-04-30 20:26:32 +0200170
Harald Welte69619e32010-05-03 19:05:10 +0200171 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200172}
173
174/* Receive an incoming signalling message from a BSS-side NS-VC */
175static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc,
176 uint16_t ns_bvci)
177{
Harald Welteca3620a2010-05-03 16:30:59 +0200178 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200179 struct tlv_parsed tp;
180 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200181 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200182 struct gbprox_peer *from_peer;
Harald Welte70f38d22010-05-01 12:10:57 +0200183 struct gprs_ra_id raid;
Harald Welte9f75c352010-04-30 20:26:32 +0200184
185 if (ns_bvci != 0) {
Harald Welte44c48302010-05-03 19:22:32 +0200186 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI %u is not signalling\n",
187 nsvc->nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200188 return -EINVAL;
189 }
190
191 /* we actually should never see those two for BVCI == 0, but double-check
192 * just to make sure */
193 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
194 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200195 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
196 "signalling\n", nsvc->nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200197 return -EINVAL;
198 }
199
200 bssgp_tlv_parse(&tp, bgph->data, data_len);
201
202 switch (pdu_type) {
203 case BSSGP_PDUT_SUSPEND:
204 case BSSGP_PDUT_RESUME:
Harald Welte70f38d22010-05-01 12:10:57 +0200205 /* We implement RAC snooping during SUSPEND/RESUME, since
206 * it establishes a relationsip between BVCI/peer and the
207 * routeing area code. The snooped information is then
208 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
209 * the correct BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200210 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
211 goto err_mand_ie;
212 from_peer = peer_by_nsvc(nsvc);
213 if (!from_peer)
214 goto err_no_peer;
215 memcpy(&from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
216 sizeof(&from_peer->ra));
Harald Welte70f38d22010-05-01 12:10:57 +0200217 gsm48_parse_ra(&raid, &from_peer->ra);
Harald Welte44c48302010-05-03 19:22:32 +0200218 DEBUGP(DGPRS, "NSEI=%u RAC snooping: RAC %u/%u/%u/%u behind BVCI=%u, "
219 "NSVCI=%u\n", nsvc->nsei, raid.mcc, raid.mnc, raid.lac,
220 raid.rac , from_peer->bvci, nsvc->nsvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200221 /* FIXME: This only supports one BSS per RA */
222 break;
Harald Welte44c48302010-05-03 19:22:32 +0200223 case BSSGP_PDUT_BVC_RESET:
224 /* If we receive a BVC reset on the signalling endpoint, we
225 * don't want the SGSN to reset, as the signalling endpoint
226 * is common for all point-to-point BVCs (and thus all BTS) */
227 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
228 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
229 if (bvci == 0) {
230 /* FIXME: only do this if SGSN is alive! */
231 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Sending fake "
232 "BVC RESET ACK of BVCI=0\n", nsvc->nsei);
233 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
234 nsvc->nsei, 0, ns_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200235 } else if (!peer_by_bvci(bvci)) {
236 /* if a PTP-BVC is reset, and we don't know that
237 * PTP-BVCI yet, we should allocate a new peer */
238 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
239 "BVCI=%u via NSVCI=%u/NSEI=%u\n", bvci,
240 nsvc->nsvci, nsvc->nsei);
241 from_peer = peer_alloc(bvci);
242 from_peer->nsvc = nsvc;
Harald Welte44c48302010-05-03 19:22:32 +0200243 }
244 }
245 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200246 }
247
248 /* Normally, we can simply pass on all signalling messages from BSS to SGSN */
Harald Welte69619e32010-05-03 19:05:10 +0200249 return gbprox_relay2sgsn(msg, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200250err_no_peer:
Harald Welte0a4050c2010-05-11 10:01:17 +0200251 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on RAC\n",
252 nsvc->nsei);
253 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200254err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200255 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
256 nsvc->nsei);
257 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200258}
259
260/* Receive paging request from SGSN, we need to relay to proper BSS */
261static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
262 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
263{
264 struct gbprox_peer *peer;
265
266 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
267 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte69619e32010-05-03 19:05:10 +0200268 return gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200269 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
270 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte69619e32010-05-03 19:05:10 +0200271 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200272 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
273 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Harald Welte69619e32010-05-03 19:05:10 +0200274 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200275 } else
276 return -EINVAL;
277}
278
Harald Welte0a4050c2010-05-11 10:01:17 +0200279/* Receive an incoming BVC-RESET message from the SGSN */
280static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp,
281 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
282{
283 struct gbprox_peer *peer;
284 uint16_t ptp_bvci;
285
286 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
287 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
288 NULL, msg);
289 }
290 ptp_bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
291
292 if (ptp_bvci >= 2) {
293 /* A reset for a PTP BVC was received, forward it to its
294 * respective peer */
295 peer = peer_by_bvci(ptp_bvci);
296 if (!peer) {
297 LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n",
298 ptp_bvci);
299 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
300 NULL, msg);
301 }
302 return gbprox_relay2peer(msg, peer, ns_bvci);
303 }
304
305 /* A reset for the Signalling entity has been received
306 * from the SGSN. As the signalling BVCI is shared
307 * among all the BSS's that we multiplex, it needs to
308 * be relayed */
309 llist_for_each_entry(peer, &gbprox_bts_peers, list)
310 gbprox_relay2peer(msg, peer, ns_bvci);
311
312 return 0;
313}
314
Harald Welte9f75c352010-04-30 20:26:32 +0200315/* Receive an incoming signalling message from the SGSN-side NS-VC */
316static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc,
317 uint16_t ns_bvci)
318{
Harald Welteca3620a2010-05-03 16:30:59 +0200319 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200320 struct tlv_parsed tp;
321 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200322 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200323 struct gbprox_peer *peer;
324 uint16_t bvci;
325 int rc = 0;
326
327 if (ns_bvci != 0) {
Harald Welte44c48302010-05-03 19:22:32 +0200328 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI %u is not "
329 "signalling\n", nsvc->nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200330 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +0200331 return -EINVAL;
332 }
333
334 /* we actually should never see those two for BVCI == 0, but double-check
335 * just to make sure */
336 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
337 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200338 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
339 "signalling\n", nsvc->nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200340 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200341 }
342
343 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
344
345 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +0200346 case BSSGP_PDUT_BVC_RESET:
347 rc = rx_reset_from_sgsn(msg, &tp, nsvc, ns_bvci);
348 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200349 case BSSGP_PDUT_FLUSH_LL:
350 case BSSGP_PDUT_BVC_BLOCK_ACK:
351 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte9f75c352010-04-30 20:26:32 +0200352 case BSSGP_PDUT_BVC_RESET_ACK:
353 /* simple case: BVCI IE is mandatory */
354 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
355 goto err_mand_ie;
356 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welte69619e32010-05-03 19:05:10 +0200357 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200358 break;
359 case BSSGP_PDUT_PAGING_PS:
360 case BSSGP_PDUT_PAGING_CS:
361 /* process the paging request (LAC/RAC lookup) */
362 rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci);
363 break;
364 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +0200365 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +0200366 LOGP(DGPRS, LOGL_NOTICE,
Harald Welte0a4050c2010-05-11 10:01:17 +0200367 "NSEI=%u(SGSN) STATUS ", nsvc->nsei);
368 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
369 LOGPC(DGPRS, LOGL_NOTICE, "\n");
370 goto err_mand_ie;
371 }
372 LOGPC(DGPRS, LOGL_NOTICE,
373 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
374 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
375 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
376 uint16_t *bvci = TLVP_VAL(&tp, BSSGP_IE_BVCI);
377 LOGPC(DGPRS, LOGL_NOTICE,
378 "BVCI=%u\n", ntohs(*bvci));
379 } else
380 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +0200381 break;
382 /* those only exist in the SGSN -> BSS direction */
383 case BSSGP_PDUT_SUSPEND_ACK:
384 case BSSGP_PDUT_SUSPEND_NACK:
385 case BSSGP_PDUT_RESUME_ACK:
386 case BSSGP_PDUT_RESUME_NACK:
387 /* RAC IE is mandatory */
388 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
389 goto err_mand_ie;
390 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
391 if (!peer)
392 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +0200393 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200394 break;
395 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +0200396 LOGP(DGPRS, LOGL_ERROR,
397 "NSEI=%u(SGSN) INVOKE TRACE not supported\n", nsvc->nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200398 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200399 break;
400 default:
401 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
Harald Welte0a4050c2010-05-11 10:01:17 +0200402 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200403 break;
404 }
405
406 return rc;
407err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +0200408 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
409 nsvc->nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200410 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200411err_no_peer:
Harald Welte0a4050c2010-05-11 10:01:17 +0200412 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n",
413 nsvc->nsei);
414 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200415}
416
417/* Main input function for Gb proxy */
418int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci)
419{
Harald Welte672f5c42010-05-03 18:54:58 +0200420 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200421
422 /* Only BVCI=0 messages need special treatment */
423 if (ns_bvci == 0 || ns_bvci == 1) {
424 if (nsvc->remote_end_is_sgsn)
Harald Welte672f5c42010-05-03 18:54:58 +0200425 rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200426 else
Harald Welte672f5c42010-05-03 18:54:58 +0200427 rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
428 } else {
429 /* All other BVCI are PTP and thus can be simply forwarded */
Harald Welte1c77c6e2010-05-03 21:37:11 +0200430 if (!nsvc->remote_end_is_sgsn) {
Harald Welte69619e32010-05-03 19:05:10 +0200431 rc = gbprox_relay2sgsn(msg, ns_bvci);
Harald Welte70f38d22010-05-01 12:10:57 +0200432 } else {
Harald Welte672f5c42010-05-03 18:54:58 +0200433 struct gbprox_peer *peer = peer_by_bvci(ns_bvci);
434 if (!peer) {
435 LOGP(DGPRS, LOGL_NOTICE, "Allocationg new peer for "
436 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
437 nsvc->nsvci, nsvc->nsei);
438 peer = peer_alloc(ns_bvci);
439 peer->nsvc = nsvc;
440 }
Harald Welte69619e32010-05-03 19:05:10 +0200441 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte70f38d22010-05-01 12:10:57 +0200442 }
Harald Welte9f75c352010-04-30 20:26:32 +0200443 }
444
Harald Welte672f5c42010-05-03 18:54:58 +0200445 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200446}
Harald Welte85801d02010-05-11 05:49:43 +0200447
Harald Weltec1c1dd22010-05-11 06:34:24 +0200448/* Signal handler for signals from NS layer */
449int gbprox_signal(unsigned int subsys, unsigned int signal,
450 void *handler_data, void *signal_data)
451{
452 struct ns_signal_data *nssd = signal_data;
453 struct gprs_nsvc *nsvc = nssd->nsvc;
454 struct gbprox_peer *peer;
455
456 if (subsys != SS_NS)
457 return 0;
458
459 /* We currently only care about signals from the SGSN */
460 if (!nsvc->remote_end_is_sgsn)
461 return 0;
462
463 /* iterate over all BTS peers and send the respective PDU */
464 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
465 switch (signal) {
466 case S_NS_RESET:
467 gprs_ns_tx_reset(peer->nsvc, nssd->cause);
468 break;
469 case S_NS_BLOCK:
470 gprs_ns_tx_block(peer->nsvc, nssd->cause);
471 break;
472 case S_NS_UNBLOCK:
473 gprs_ns_tx_unblock(peer->nsvc);
474 break;
475 }
476 }
477 return 0;
478}
479
Harald Welte85801d02010-05-11 05:49:43 +0200480
481#include <vty/command.h>
482
483gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy",
484 SHOW_STR "Display information about the Gb proxy")
485{
486 struct gbprox_peer *peer;
487
488 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
489 struct gprs_nsvc *nsvc = peer->nsvc;
490 struct gprs_ra_id raid;
491 gsm48_parse_ra(&raid, &peer->ra);
492
493 vty_out(vty, "NSEI %5u, NS-VC %5u, PTP-BVCI %u, "
494 "RAC %u-%u-%u-%u%s",
495 nsvc->nsei, nsvc->nsvci, peer->bvci,
496 raid.mcc, raid.mnc, raid.lac, raid.rac, VTY_NEWLINE);
497 if (nsvc->nsi->ll == GPRS_NS_LL_UDP)
498 vty_out(vty, " remote address %s:%u%s",
499 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
500 ntohs(nsvc->ip.bts_addr.sin_port), VTY_NEWLINE);
501 }
502 return CMD_SUCCESS;
503}