blob: a4b432e80cec143b6fc3ab563f4c439e0dc197b0 [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 +020058extern struct gprs_ns_inst *gbprox_nsi;
59
60/* Find the gbprox_peer by its BVCI */
61static struct gbprox_peer *peer_by_bvci(uint16_t bvci)
62{
63 struct gbprox_peer *peer;
64 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
65 if (peer->bvci == bvci)
66 return peer;
67 }
68 return NULL;
69}
70
71static struct gbprox_peer *peer_by_nsvc(struct gprs_nsvc *nsvc)
72{
73 struct gbprox_peer *peer;
74 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
75 if (peer->nsvc == nsvc)
76 return peer;
77 }
78 return NULL;
79}
80
81/* look-up a peer by its Routeing Area Code (RAC) */
Harald Welte70f38d22010-05-01 12:10:57 +020082static struct gbprox_peer *peer_by_rac(const uint8_t *ra)
Harald Welte9f75c352010-04-30 20:26:32 +020083{
84 struct gbprox_peer *peer;
85 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
86 if (!memcmp(&peer->ra, ra, 6))
87 return peer;
88 }
89 return NULL;
90}
91
92/* look-up a peer by its Location Area Code (LAC) */
Harald Welte70f38d22010-05-01 12:10:57 +020093static struct gbprox_peer *peer_by_lac(const uint8_t *la)
Harald Welte9f75c352010-04-30 20:26:32 +020094{
95 struct gbprox_peer *peer;
96 llist_for_each_entry(peer, &gbprox_bts_peers, list) {
97 if (!memcmp(&peer->ra, la, 5))
98 return peer;
99 }
100 return NULL;
101}
102
103static struct gbprox_peer *peer_alloc(uint16_t bvci)
104{
105 struct gbprox_peer *peer;
106
107 peer = talloc_zero(tall_bsc_ctx, struct gbprox_peer);
108 if (!peer)
109 return NULL;
110
111 peer->bvci = bvci;
112 llist_add(&peer->list, &gbprox_bts_peers);
113
114 return peer;
115}
116
117static void peer_free(struct gbprox_peer *peer)
118{
119 llist_del(&peer->list);
120 talloc_free(peer);
121}
122
Harald Welte69619e32010-05-03 19:05:10 +0200123/* strip off the NS header */
124static void strip_ns_hdr(struct msgb *msg)
125{
126 int strip_len = msgb_bssgph(msg) - msg->data;
127 msgb_pull(msg, strip_len);
128}
129
Harald Welte44c48302010-05-03 19:22:32 +0200130/* FIXME: this is copy+paste from gprs_bssgp.c */
131static inline struct msgb *bssgp_msgb_alloc(void)
132{
133 return msgb_alloc_headroom(4096, 128, "BSSGP");
134}
135static int bssgp_tx_simple_bvci(uint8_t pdu_type, uint16_t nsei,
136 uint16_t bvci, uint16_t ns_bvci)
137{
138 struct msgb *msg = bssgp_msgb_alloc();
139 struct bssgp_normal_hdr *bgph =
140 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
141 uint16_t _bvci;
142
143 msgb_nsei(msg) = nsei;
144 msgb_bvci(msg) = ns_bvci;
145
146 bgph->pdu_type = pdu_type;
147 _bvci = htons(bvci);
148 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
149
150 return gprs_ns_sendmsg(gbprox_nsi, msg);
151}
152
153
Harald Welte9f75c352010-04-30 20:26:32 +0200154/* feed a message down the NS-VC associated with the specified peer */
Harald Welte69619e32010-05-03 19:05:10 +0200155static int gbprox_relay2sgsn(struct msgb *msg, uint16_t ns_bvci)
Harald Welte672f5c42010-05-03 18:54:58 +0200156{
Harald Welte44c48302010-05-03 19:22:32 +0200157 DEBUGP(DGPRS, "NSEI=%u proxying to SGSN (NS_BVCI=%u, NSEI=%u)\n",
Harald Welte96f71f22010-05-03 19:28:05 +0200158 msgb_nsei(msg), ns_bvci, gbcfg.nsip_sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200159
Harald Welte672f5c42010-05-03 18:54:58 +0200160 msgb_bvci(msg) = ns_bvci;
161 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
162
Harald Welte69619e32010-05-03 19:05:10 +0200163 strip_ns_hdr(msg);
164
Harald Welte672f5c42010-05-03 18:54:58 +0200165 return gprs_ns_sendmsg(gbprox_nsi, msg);
166}
167
Harald Welte672f5c42010-05-03 18:54:58 +0200168/* feed a message down the NS-VC associated with the specified peer */
Harald Welte69619e32010-05-03 19:05:10 +0200169static int gbprox_relay2peer(struct msgb *msg, struct gbprox_peer *peer,
Harald Welte9f75c352010-04-30 20:26:32 +0200170 uint16_t ns_bvci)
171{
Harald Welte44c48302010-05-03 19:22:32 +0200172 DEBUGP(DGPRS, "NSEI=%u proxying to to BSS (NS_BVCI=%u, NSEI=%u)\n",
Harald Welte96f71f22010-05-03 19:28:05 +0200173 msgb_nsei(msg), ns_bvci, peer->nsvc->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200174
Harald Welte9f75c352010-04-30 20:26:32 +0200175 msgb_bvci(msg) = ns_bvci;
176 msgb_nsei(msg) = peer->nsvc->nsei;
177
Harald Welte69619e32010-05-03 19:05:10 +0200178 strip_ns_hdr(msg);
179
Harald Welte9f75c352010-04-30 20:26:32 +0200180 return gprs_ns_sendmsg(gbprox_nsi, msg);
181}
182
183/* Send a message to a peer identified by ptp_bvci but using ns_bvci
184 * in the NS hdr */
Harald Welte69619e32010-05-03 19:05:10 +0200185static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200186 uint16_t ns_bvci)
187{
188 struct gbprox_peer *peer;
189
190 peer = peer_by_bvci(ptp_bvci);
191 if (!peer)
192 return -ENOENT;
193
Harald Welte69619e32010-05-03 19:05:10 +0200194 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200195}
196
197/* Receive an incoming signalling message from a BSS-side NS-VC */
198static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc,
199 uint16_t ns_bvci)
200{
Harald Welteca3620a2010-05-03 16:30:59 +0200201 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200202 struct tlv_parsed tp;
203 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200204 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200205 struct gbprox_peer *from_peer;
Harald Welte70f38d22010-05-01 12:10:57 +0200206 struct gprs_ra_id raid;
Harald Welte9f75c352010-04-30 20:26:32 +0200207
208 if (ns_bvci != 0) {
Harald Welte44c48302010-05-03 19:22:32 +0200209 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u BVCI %u is not signalling\n",
210 nsvc->nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200211 return -EINVAL;
212 }
213
214 /* we actually should never see those two for BVCI == 0, but double-check
215 * just to make sure */
216 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
217 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200218 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u UNITDATA not allowed in "
219 "signalling\n", nsvc->nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200220 return -EINVAL;
221 }
222
223 bssgp_tlv_parse(&tp, bgph->data, data_len);
224
225 switch (pdu_type) {
226 case BSSGP_PDUT_SUSPEND:
227 case BSSGP_PDUT_RESUME:
Harald Welte70f38d22010-05-01 12:10:57 +0200228 /* We implement RAC snooping during SUSPEND/RESUME, since
229 * it establishes a relationsip between BVCI/peer and the
230 * routeing area code. The snooped information is then
231 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
232 * the correct BSSGP */
Harald Welte9f75c352010-04-30 20:26:32 +0200233 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
234 goto err_mand_ie;
235 from_peer = peer_by_nsvc(nsvc);
236 if (!from_peer)
237 goto err_no_peer;
238 memcpy(&from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
239 sizeof(&from_peer->ra));
Harald Welte70f38d22010-05-01 12:10:57 +0200240 gsm48_parse_ra(&raid, &from_peer->ra);
Harald Welte44c48302010-05-03 19:22:32 +0200241 DEBUGP(DGPRS, "NSEI=%u RAC snooping: RAC %u/%u/%u/%u behind BVCI=%u, "
242 "NSVCI=%u\n", nsvc->nsei, raid.mcc, raid.mnc, raid.lac,
243 raid.rac , from_peer->bvci, nsvc->nsvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200244 /* FIXME: This only supports one BSS per RA */
245 break;
Harald Welte44c48302010-05-03 19:22:32 +0200246 case BSSGP_PDUT_BVC_RESET:
247 /* If we receive a BVC reset on the signalling endpoint, we
248 * don't want the SGSN to reset, as the signalling endpoint
249 * is common for all point-to-point BVCs (and thus all BTS) */
250 if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
251 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
252 if (bvci == 0) {
253 /* FIXME: only do this if SGSN is alive! */
254 LOGP(DGPRS, LOGL_INFO, "NSEI=%u Sending fake "
255 "BVC RESET ACK of BVCI=0\n", nsvc->nsei);
256 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
257 nsvc->nsei, 0, ns_bvci);
258 }
259 }
260 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200261 }
262
263 /* Normally, we can simply pass on all signalling messages from BSS to SGSN */
Harald Welte69619e32010-05-03 19:05:10 +0200264 return gbprox_relay2sgsn(msg, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200265err_no_peer:
266err_mand_ie:
267 /* FIXME: do something */
268 ;
269}
270
271/* Receive paging request from SGSN, we need to relay to proper BSS */
272static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
273 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
274{
275 struct gbprox_peer *peer;
276
277 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
278 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte69619e32010-05-03 19:05:10 +0200279 return gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200280 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
281 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte69619e32010-05-03 19:05:10 +0200282 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200283 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
284 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Harald Welte69619e32010-05-03 19:05:10 +0200285 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200286 } else
287 return -EINVAL;
288}
289
290/* Receive an incoming signalling message from the SGSN-side NS-VC */
291static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc,
292 uint16_t ns_bvci)
293{
Harald Welteca3620a2010-05-03 16:30:59 +0200294 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200295 struct tlv_parsed tp;
296 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200297 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200298 struct gbprox_peer *peer;
299 uint16_t bvci;
300 int rc = 0;
301
302 if (ns_bvci != 0) {
Harald Welte44c48302010-05-03 19:22:32 +0200303 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) BVCI %u is not "
304 "signalling\n", nsvc->nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200305 return -EINVAL;
306 }
307
308 /* we actually should never see those two for BVCI == 0, but double-check
309 * just to make sure */
310 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
311 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Harald Welte44c48302010-05-03 19:22:32 +0200312 LOGP(DGPRS, LOGL_NOTICE, "NSEI=%u(SGSN) UNITDATA not allowed in "
313 "signalling\n", nsvc->nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200314 return -EINVAL;
315 }
316
317 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
318
319 switch (pdu_type) {
320 case BSSGP_PDUT_FLUSH_LL:
321 case BSSGP_PDUT_BVC_BLOCK_ACK:
322 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
323 case BSSGP_PDUT_BVC_RESET:
324 case BSSGP_PDUT_BVC_RESET_ACK:
325 /* simple case: BVCI IE is mandatory */
326 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
327 goto err_mand_ie;
328 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welte69619e32010-05-03 19:05:10 +0200329 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200330 break;
331 case BSSGP_PDUT_PAGING_PS:
332 case BSSGP_PDUT_PAGING_CS:
333 /* process the paging request (LAC/RAC lookup) */
334 rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci);
335 break;
336 case BSSGP_PDUT_STATUS:
337 /* FIXME: Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +0200338 LOGP(DGPRS, LOGL_NOTICE,
339 "NSEI=%u(SGSN) STATUS not implemented yet\n", nsvc->nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200340 break;
341 /* those only exist in the SGSN -> BSS direction */
342 case BSSGP_PDUT_SUSPEND_ACK:
343 case BSSGP_PDUT_SUSPEND_NACK:
344 case BSSGP_PDUT_RESUME_ACK:
345 case BSSGP_PDUT_RESUME_NACK:
346 /* RAC IE is mandatory */
347 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
348 goto err_mand_ie;
349 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
350 if (!peer)
351 goto err_no_peer;
Harald Welte69619e32010-05-03 19:05:10 +0200352 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200353 break;
354 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte44c48302010-05-03 19:22:32 +0200355 LOGP(DGPRS, LOGL_ERROR,
356 "NSEI=%u(SGSN) INVOKE TRACE not supported\n", nsvc->nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200357 break;
358 default:
359 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
360 break;
361 }
362
363 return rc;
364err_mand_ie:
365 ; /* FIXME: this would pull gprs_bssgp.c in, which in turn has dependencies */
366 //return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
367err_no_peer:
368 ; /* FIXME */
369}
370
371/* Main input function for Gb proxy */
372int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci)
373{
Harald Welte672f5c42010-05-03 18:54:58 +0200374 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200375
376 /* Only BVCI=0 messages need special treatment */
377 if (ns_bvci == 0 || ns_bvci == 1) {
378 if (nsvc->remote_end_is_sgsn)
Harald Welte672f5c42010-05-03 18:54:58 +0200379 rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200380 else
Harald Welte672f5c42010-05-03 18:54:58 +0200381 rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
382 } else {
383 /* All other BVCI are PTP and thus can be simply forwarded */
384 if (nsvc->remote_end_is_sgsn) {
Harald Welte69619e32010-05-03 19:05:10 +0200385 rc = gbprox_relay2sgsn(msg, ns_bvci);
Harald Welte70f38d22010-05-01 12:10:57 +0200386 } else {
Harald Welte672f5c42010-05-03 18:54:58 +0200387 struct gbprox_peer *peer = peer_by_bvci(ns_bvci);
388 if (!peer) {
389 LOGP(DGPRS, LOGL_NOTICE, "Allocationg new peer for "
390 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
391 nsvc->nsvci, nsvc->nsei);
392 peer = peer_alloc(ns_bvci);
393 peer->nsvc = nsvc;
394 }
Harald Welte69619e32010-05-03 19:05:10 +0200395 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welte70f38d22010-05-01 12:10:57 +0200396 }
Harald Welte9f75c352010-04-30 20:26:32 +0200397 }
398
Harald Welte672f5c42010-05-03 18:54:58 +0200399 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200400}