blob: ea992e9507e514f2ead47335d110b69230c81148 [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>
Harald Welte7fc98222010-05-11 10:15:42 +020032#include <arpa/inet.h>
Harald Welte9f75c352010-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 Welte672f5c42010-05-03 18:54:58 +020041#include <openbsc/gb_proxy.h>
Harald Welte9f75c352010-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 Welte9f75c352010-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 Welte70f38d22010-05-01 12:10:57 +020081static struct gbprox_peer *peer_by_rac(const uint8_t *ra)
Harald Welte9f75c352010-04-30 20:26:32 +020082{
83 struct gbprox_peer *peer;
84 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
85 if (!memcmp(&peer->ra, ra, 6))
86 return peer;
87 }
88 return NULL;
89}
90
91/* look-up a peer by its Location Area Code (LAC) */
Harald Welte70f38d22010-05-01 12:10:57 +020092static struct gbprox_peer *peer_by_lac(const uint8_t *la)
Harald Welte9f75c352010-04-30 20:26:32 +020093{
94 struct gbprox_peer *peer;
95 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
96 if (!memcmp(&peer->ra, la, 5))
97 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 Welte69619e32010-05-03 19:05:10 +0200122/* strip off the NS header */
123static void strip_ns_hdr(struct msgb *msg)
124{
125 int strip_len = msgb_bssgph(msg) - msg->data;
126 msgb_pull(msg, strip_len);
127}
128
Harald Welte9f75c352010-04-30 20:26:32 +0200129/* feed a message down the NS-VC associated with the specified peer */
Harald Welte69619e32010-05-03 19:05:10 +0200130static int gbprox_relay2sgsn(struct msgb *msg, uint16_t ns_bvci)
Harald Welte672f5c42010-05-03 18:54:58 +0200131{
Harald Welte44c48302010-05-03 19:22:32 +0200132 DEBUGP(DGPRS, "NSEI=%u proxying to SGSN (NS_BVCI=%u, NSEI=%u)\n",
Harald Welte96f71f22010-05-03 19:28:05 +0200133 msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200134
Harald Welte672f5c42010-05-03 18:54:58 +0200135 msgb_bvci(msg) = ns_bvci;
136 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
137
Harald Welte69619e32010-05-03 19:05:10 +0200138 strip_ns_hdr(msg);
139
Harald Welte0a4050c2010-05-11 10:01:17 +0200140 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte672f5c42010-05-03 18:54:58 +0200141}
142
Harald Welte672f5c42010-05-03 18:54:58 +0200143/* feed a message down the NS-VC associated with the specified peer */
Harald Welte69619e32010-05-03 19:05:10 +0200144static int gbprox_relay2peer(struct msgb *msg, struct gbprox_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200145 uint16_t ns_bvci)
146{
Harald Welte1c77c6e2010-05-03 21:37:11 +0200147 DEBUGP(DGPRS, "NSEI=%u proxying to BSS (NS_BVCI=%u, NSEI=%u)\n",
Harald Welte96f71f22010-05-03 19:28:05 +0200148 msgb_nsei(msg), ns_bvci, peer->nsvc->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200149
Harald Welte9f75c352010-04-30 20:26:32 +0200150 msgb_bvci(msg) = ns_bvci;
151 msgb_nsei(msg) = peer->nsvc->nsei;
152
Harald Welte69619e32010-05-03 19:05:10 +0200153 strip_ns_hdr(msg);
154
Harald Welte0a4050c2010-05-11 10:01:17 +0200155 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200156}
157
158/* Send a message to a peer identified by ptp_bvci but using ns_bvci
159 * in the NS hdr */
Harald Welte69619e32010-05-03 19:05:10 +0200160static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200161 uint16_t ns_bvci)
162{
163 struct gbprox_peer *peer;
164
165 peer = peer_by_bvci(ptp_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200166 if (!peer) {
167 LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n",
168 ptp_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200169 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200170 }
Harald Welte9f75c352010-04-30 20:26:32 +0200171
Harald Welte69619e32010-05-03 19:05:10 +0200172 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200173}
174
175/* Receive an incoming signalling message from a BSS-side NS-VC */
176static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc,
177 uint16_t ns_bvci)
178{
Harald Welteca3620a2010-05-03 16:30:59 +0200179 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200180 struct tlv_parsed tp;
181 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200182 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200183 struct gbprox_peer *from_peer;
Harald Welte70f38d22010-05-01 12:10:57 +0200184 struct gprs_ra_id raid;
Harald Welte9f75c352010-04-30 20:26:32 +0200185
186 if (ns_bvci != 0) {
Harald Welte44c48302010-05-03 19:22:32 +0200187 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI %u is not signalling\n",
188 nsvc->nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200189 return -EINVAL;
190 }
191
192 /* we actually should never see those two for BVCI == 0, but double-check
193 * just to make sure */
194 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
195 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200196 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
197 "signalling\n", nsvc->nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200198 return -EINVAL;
199 }
200
201 bssgp_tlv_parse(&tp, bgph->data, data_len);
202
203 switch (pdu_type) {
204 case BSSGP_PDUT_SUSPEND:
205 case BSSGP_PDUT_RESUME:
Harald Welte70f38d22010-05-01 12:10:57 +0200206 /* We implement RAC snooping during SUSPEND/RESUME, since
207 * it establishes a relationsip between BVCI/peer and the
208 * routeing area code. The snooped information is then
209 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
210 * the correct BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200211 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
212 goto err_mand_ie;
213 from_peer = peer_by_nsvc(nsvc);
214 if (!from_peer)
215 goto err_no_peer;
216 memcpy(&from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
217 sizeof(&from_peer->ra));
Harald Welte7fc98222010-05-11 10:15:42 +0200218 gsm48_parse_ra(&raid, from_peer->ra);
Harald Welte44c48302010-05-03 19:22:32 +0200219 DEBUGP(DGPRS, "NSEI=%u RAC snooping: RAC %u/%u/%u/%u behind BVCI=%u, "
220 "NSVCI=%u\n", nsvc->nsei, raid.mcc, raid.mnc, raid.lac,
221 raid.rac , from_peer->bvci, nsvc->nsvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200222 /* FIXME: This only supports one BSS per RA */
223 break;
Harald Welte44c48302010-05-03 19:22:32 +0200224 case BSSGP_PDUT_BVC_RESET:
225 /* If we receive a BVC reset on the signalling endpoint, we
226 * don't want the SGSN to reset, as the signalling endpoint
227 * is common for all point-to-point BVCs (and thus all BTS) */
228 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
229 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
230 if (bvci == 0) {
231 /* FIXME: only do this if SGSN is alive! */
232 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Sending fake "
233 "BVC RESET ACK of BVCI=0\n", nsvc->nsei);
234 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
235 nsvc->nsei, 0, ns_bvci);
Harald Welte1c77c6e2010-05-03 21:37:11 +0200236 } else if (!peer_by_bvci(bvci)) {
237 /* if a PTP-BVC is reset, and we don't know that
238 * PTP-BVCI yet, we should allocate a new peer */
239 LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
240 "BVCI=%u via NSVCI=%u/NSEI=%u\n", bvci,
241 nsvc->nsvci, nsvc->nsei);
242 from_peer = peer_alloc(bvci);
243 from_peer->nsvc = nsvc;
Harald Welte44c48302010-05-03 19:22:32 +0200244 }
245 }
246 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200247 }
248
249 /* Normally, we can simply pass on all signalling messages from BSS to SGSN */
Harald Welte69619e32010-05-03 19:05:10 +0200250 return gbprox_relay2sgsn(msg, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200251err_no_peer:
Harald Welte0a4050c2010-05-11 10:01:17 +0200252 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) cannot find peer based on RAC\n",
253 nsvc->nsei);
254 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200255err_mand_ie:
Harald Welte0a4050c2010-05-11 10:01:17 +0200256 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(BSS) missing mandatory RA IE\n",
257 nsvc->nsei);
258 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200259}
260
261/* Receive paging request from SGSN, we need to relay to proper BSS */
262static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
263 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
264{
265 struct gbprox_peer *peer;
266
267 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
268 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte69619e32010-05-03 19:05:10 +0200269 return gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200270 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
271 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte69619e32010-05-03 19:05:10 +0200272 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200273 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
274 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Harald Welte69619e32010-05-03 19:05:10 +0200275 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200276 } else
277 return -EINVAL;
278}
279
Harald Welte0a4050c2010-05-11 10:01:17 +0200280/* Receive an incoming BVC-RESET message from the SGSN */
281static int rx_reset_from_sgsn(struct msgb *msg, struct tlv_parsed *tp,
282 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
283{
284 struct gbprox_peer *peer;
285 uint16_t ptp_bvci;
286
287 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
288 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
289 NULL, msg);
290 }
291 ptp_bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
292
293 if (ptp_bvci >= 2) {
294 /* A reset for a PTP BVC was received, forward it to its
295 * respective peer */
296 peer = peer_by_bvci(ptp_bvci);
297 if (!peer) {
298 LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n",
299 ptp_bvci);
300 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
301 NULL, msg);
302 }
303 return gbprox_relay2peer(msg, peer, ns_bvci);
304 }
305
306 /* A reset for the Signalling entity has been received
307 * from the SGSN. As the signalling BVCI is shared
308 * among all the BSS's that we multiplex, it needs to
309 * be relayed */
310 llist_for_each_entry(peer, &gbprox_bts_peers, list)
311 gbprox_relay2peer(msg, peer, ns_bvci);
312
313 return 0;
314}
315
Harald Welte9f75c352010-04-30 20:26:32 +0200316/* Receive an incoming signalling message from the SGSN-side NS-VC */
317static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc,
318 uint16_t ns_bvci)
319{
Harald Welteca3620a2010-05-03 16:30:59 +0200320 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200321 struct tlv_parsed tp;
322 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200323 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200324 struct gbprox_peer *peer;
325 uint16_t bvci;
326 int rc = 0;
327
328 if (ns_bvci != 0) {
Harald Welte44c48302010-05-03 19:22:32 +0200329 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI %u is not "
330 "signalling\n", nsvc->nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200331 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +0200332 return -EINVAL;
333 }
334
335 /* we actually should never see those two for BVCI == 0, but double-check
336 * just to make sure */
337 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
338 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200339 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
340 "signalling\n", nsvc->nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200341 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200342 }
343
344 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
345
346 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +0200347 case BSSGP_PDUT_BVC_RESET:
348 rc = rx_reset_from_sgsn(msg, &tp, nsvc, ns_bvci);
349 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200350 case BSSGP_PDUT_FLUSH_LL:
351 case BSSGP_PDUT_BVC_BLOCK_ACK:
352 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte9f75c352010-04-30 20:26:32 +0200353 case BSSGP_PDUT_BVC_RESET_ACK:
354 /* simple case: BVCI IE is mandatory */
355 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
356 goto err_mand_ie;
357 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welte69619e32010-05-03 19:05:10 +0200358 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200359 break;
360 case BSSGP_PDUT_PAGING_PS:
361 case BSSGP_PDUT_PAGING_CS:
362 /* process the paging request (LAC/RAC lookup) */
363 rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci);
364 break;
365 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +0200366 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +0200367 LOGP(DGPRS, LOGL_NOTICE,
Harald Welte0a4050c2010-05-11 10:01:17 +0200368 "NSEI=%u(SGSN) STATUS ", nsvc->nsei);
369 if (!TLVP_PRESENT(&tp, BSSGP_IE_CAUSE)) {
370 LOGPC(DGPRS, LOGL_NOTICE, "\n");
371 goto err_mand_ie;
372 }
373 LOGPC(DGPRS, LOGL_NOTICE,
374 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
375 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
376 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
Harald Welte7fc98222010-05-11 10:15:42 +0200377 uint16_t *bvci = (uint16_t *)
378 TLVP_VAL(&tp, BSSGP_IE_BVCI);
Harald Welte0a4050c2010-05-11 10:01:17 +0200379 LOGPC(DGPRS, LOGL_NOTICE,
380 "BVCI=%u\n", ntohs(*bvci));
381 } else
382 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +0200383 break;
384 /* those only exist in the SGSN -> BSS direction */
385 case BSSGP_PDUT_SUSPEND_ACK:
386 case BSSGP_PDUT_SUSPEND_NACK:
387 case BSSGP_PDUT_RESUME_ACK:
388 case BSSGP_PDUT_RESUME_NACK:
389 /* RAC IE is mandatory */
390 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
391 goto err_mand_ie;
392 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
393 if (!peer)
394 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +0200395 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200396 break;
397 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +0200398 LOGP(DGPRS, LOGL_ERROR,
399 "NSEI=%u(SGSN) INVOKE TRACE not supported\n", nsvc->nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200400 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200401 break;
402 default:
403 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
Harald Welte0a4050c2010-05-11 10:01:17 +0200404 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200405 break;
406 }
407
408 return rc;
409err_mand_ie:
Harald Welte1c77c6e2010-05-03 21:37:11 +0200410 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
411 nsvc->nsei);
Harald Welte0a4050c2010-05-11 10:01:17 +0200412 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200413err_no_peer:
Harald Welte0a4050c2010-05-11 10:01:17 +0200414 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n",
415 nsvc->nsei);
416 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200417}
418
419/* Main input function for Gb proxy */
420int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci)
421{
Harald Welte672f5c42010-05-03 18:54:58 +0200422 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200423
424 /* Only BVCI=0 messages need special treatment */
425 if (ns_bvci == 0 || ns_bvci == 1) {
426 if (nsvc->remote_end_is_sgsn)
Harald Welte672f5c42010-05-03 18:54:58 +0200427 rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200428 else
Harald Welte672f5c42010-05-03 18:54:58 +0200429 rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
430 } else {
431 /* All other BVCI are PTP and thus can be simply forwarded */
Harald Welte1c77c6e2010-05-03 21:37:11 +0200432 if (!nsvc->remote_end_is_sgsn) {
Harald Welte69619e32010-05-03 19:05:10 +0200433 rc = gbprox_relay2sgsn(msg, ns_bvci);
Harald Welte70f38d22010-05-01 12:10:57 +0200434 } else {
Harald Welte672f5c42010-05-03 18:54:58 +0200435 struct gbprox_peer *peer = peer_by_bvci(ns_bvci);
436 if (!peer) {
437 LOGP(DGPRS, LOGL_NOTICE, "Allocationg new peer for "
438 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
439 nsvc->nsvci, nsvc->nsei);
440 peer = peer_alloc(ns_bvci);
441 peer->nsvc = nsvc;
442 }
Harald Welte69619e32010-05-03 19:05:10 +0200443 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte70f38d22010-05-01 12:10:57 +0200444 }
Harald Welte9f75c352010-04-30 20:26:32 +0200445 }
446
Harald Welte672f5c42010-05-03 18:54:58 +0200447 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200448}
Harald Welte85801d02010-05-11 05:49:43 +0200449
Harald Weltec1c1dd22010-05-11 06:34:24 +0200450/* Signal handler for signals from NS layer */
451int gbprox_signal(unsigned int subsys, unsigned int signal,
452 void *handler_data, void *signal_data)
453{
454 struct ns_signal_data *nssd = signal_data;
455 struct gprs_nsvc *nsvc = nssd->nsvc;
456 struct gbprox_peer *peer;
457
458 if (subsys != SS_NS)
459 return 0;
460
461 /* We currently only care about signals from the SGSN */
462 if (!nsvc->remote_end_is_sgsn)
463 return 0;
464
465 /* iterate over all BTS peers and send the respective PDU */
466 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
467 switch (signal) {
468 case S_NS_RESET:
469 gprs_ns_tx_reset(peer->nsvc, nssd->cause);
470 break;
471 case S_NS_BLOCK:
472 gprs_ns_tx_block(peer->nsvc, nssd->cause);
473 break;
474 case S_NS_UNBLOCK:
475 gprs_ns_tx_unblock(peer->nsvc);
476 break;
477 }
478 }
479 return 0;
480}
481
Harald Welte85801d02010-05-11 05:49:43 +0200482
483#include <vty/command.h>
484
485gDEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy",
486 SHOW_STR "Display information about the Gb proxy")
487{
488 struct gbprox_peer *peer;
489
490 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
491 struct gprs_nsvc *nsvc = peer->nsvc;
492 struct gprs_ra_id raid;
Harald Welte7fc98222010-05-11 10:15:42 +0200493 gsm48_parse_ra(&raid, peer->ra);
Harald Welte85801d02010-05-11 05:49:43 +0200494
495 vty_out(vty, "NSEI %5u, NS-VC %5u, PTP-BVCI %u, "
496 "RAC %u-%u-%u-%u%s",
497 nsvc->nsei, nsvc->nsvci, peer->bvci,
498 raid.mcc, raid.mnc, raid.lac, raid.rac, VTY_NEWLINE);
499 if (nsvc->nsi->ll == GPRS_NS_LL_UDP)
500 vty_out(vty, " remote address %s:%u%s",
501 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
502 ntohs(nsvc->ip.bts_addr.sin_port), VTY_NEWLINE);
503 }
504 return CMD_SUCCESS;
505}