blob: 93b8f67193f851f8f3efd0cac1474b1df7fb1e5d [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>
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 Weltef01709d2010-05-03 18:54:58 +020040#include <openbsc/gb_proxy.h>
Harald Welted1991e72010-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 Welted1991e72010-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 Weltee49c8292010-05-01 12:10:57 +020082static struct gbprox_peer *peer_by_rac(const uint8_t *ra)
Harald Welted1991e72010-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 Weltee49c8292010-05-01 12:10:57 +020093static struct gbprox_peer *peer_by_lac(const uint8_t *la)
Harald Welted1991e72010-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
123/* feed a message down the NS-VC associated with the specified peer */
Harald Weltef01709d2010-05-03 18:54:58 +0200124static int gbprox_tx2sgsn(struct msgb *msg, uint16_t ns_bvci)
125{
126 msgb_bvci(msg) = ns_bvci;
127 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
128
129 DEBUGP(DGPRS, "proxying message to SGSN (NS_BVCI=%u, NSEI=%u)\n",
130 ns_bvci, gbcfg.nsip_sgsn_nsei);
131
132 return gprs_ns_sendmsg(gbprox_nsi, msg);
133}
134
135
136/* feed a message down the NS-VC associated with the specified peer */
Harald Welted1991e72010-04-30 20:26:32 +0200137static int gbprox_tx2peer(struct msgb *msg, struct gbprox_peer *peer,
138 uint16_t ns_bvci)
139{
140 msgb_bvci(msg) = ns_bvci;
141 msgb_nsei(msg) = peer->nsvc->nsei;
142
Harald Weltef01709d2010-05-03 18:54:58 +0200143 DEBUGP(DGPRS, "proxying message to BSS (NS_BVCI=%u, NSEI=%u)\n",
144 ns_bvci, peer->nsvc->nsei);
145
Harald Welted1991e72010-04-30 20:26:32 +0200146 return gprs_ns_sendmsg(gbprox_nsi, msg);
147}
148
149/* Send a message to a peer identified by ptp_bvci but using ns_bvci
150 * in the NS hdr */
151static int gbprox_tx2bvci(struct msgb *msg, uint16_t ptp_bvci,
152 uint16_t ns_bvci)
153{
154 struct gbprox_peer *peer;
155
156 peer = peer_by_bvci(ptp_bvci);
157 if (!peer)
158 return -ENOENT;
159
160 return gbprox_tx2peer(msg, peer, ns_bvci);
161}
162
163/* Receive an incoming signalling message from a BSS-side NS-VC */
164static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc,
165 uint16_t ns_bvci)
166{
Harald Welte2ac2e912010-05-03 16:30:59 +0200167 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200168 struct tlv_parsed tp;
169 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200170 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +0200171 struct gbprox_peer *from_peer;
Harald Weltee49c8292010-05-01 12:10:57 +0200172 struct gprs_ra_id raid;
Harald Welted1991e72010-04-30 20:26:32 +0200173
174 if (ns_bvci != 0) {
Harald Weltee49c8292010-05-01 12:10:57 +0200175 LOGP(DGPRS, LOGL_NOTICE, "BVCI %u is not signalling\n", ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200176 return -EINVAL;
177 }
178
179 /* we actually should never see those two for BVCI == 0, but double-check
180 * just to make sure */
181 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
182 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
183 LOGP(DGPRS, LOGL_NOTICE, "UNITDATA not allowed in signalling\n");
184 return -EINVAL;
185 }
186
187 bssgp_tlv_parse(&tp, bgph->data, data_len);
188
189 switch (pdu_type) {
190 case BSSGP_PDUT_SUSPEND:
191 case BSSGP_PDUT_RESUME:
Harald Weltee49c8292010-05-01 12:10:57 +0200192 /* We implement RAC snooping during SUSPEND/RESUME, since
193 * it establishes a relationsip between BVCI/peer and the
194 * routeing area code. The snooped information is then
195 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
196 * the correct BSSGP */
Harald Welted1991e72010-04-30 20:26:32 +0200197 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
198 goto err_mand_ie;
199 from_peer = peer_by_nsvc(nsvc);
200 if (!from_peer)
201 goto err_no_peer;
202 memcpy(&from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
203 sizeof(&from_peer->ra));
Harald Weltee49c8292010-05-01 12:10:57 +0200204 gsm48_parse_ra(&raid, &from_peer->ra);
205 DEBUGP(DGPRS, "RAC snooping: RAC %u/%u/%u/%u behind BVCI=%u, "
206 "NSVCI=%u, NSEI=%u\n", raid.mcc, raid.mnc, raid.lac,
207 raid.rac , from_peer->bvci, nsvc->nsvci, nsvc->nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200208 /* FIXME: This only supports one BSS per RA */
209 break;
210 }
211
212 /* Normally, we can simply pass on all signalling messages from BSS to SGSN */
Harald Weltef01709d2010-05-03 18:54:58 +0200213 return gbprox_tx2sgsn(msg, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200214err_no_peer:
215err_mand_ie:
216 /* FIXME: do something */
217 ;
218}
219
220/* Receive paging request from SGSN, we need to relay to proper BSS */
221static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
222 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
223{
224 struct gbprox_peer *peer;
225
226 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
227 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
228 return gbprox_tx2bvci(msg, bvci, ns_bvci);
229 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
230 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
231 return gbprox_tx2peer(msg, peer, ns_bvci);
232 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
233 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
234 return gbprox_tx2peer(msg, peer, ns_bvci);
235 } else
236 return -EINVAL;
237}
238
239/* Receive an incoming signalling message from the SGSN-side NS-VC */
240static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc,
241 uint16_t ns_bvci)
242{
Harald Welte2ac2e912010-05-03 16:30:59 +0200243 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200244 struct tlv_parsed tp;
245 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200246 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +0200247 struct gbprox_peer *peer;
248 uint16_t bvci;
249 int rc = 0;
250
251 if (ns_bvci != 0) {
Harald Weltee49c8292010-05-01 12:10:57 +0200252 LOGP(DGPRS, LOGL_NOTICE, "BVCI %u is not signalling\n", ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200253 return -EINVAL;
254 }
255
256 /* we actually should never see those two for BVCI == 0, but double-check
257 * just to make sure */
258 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
259 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
260 LOGP(DGPRS, LOGL_NOTICE, "UNITDATA not allowed in signalling\n");
261 return -EINVAL;
262 }
263
264 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
265
266 switch (pdu_type) {
267 case BSSGP_PDUT_FLUSH_LL:
268 case BSSGP_PDUT_BVC_BLOCK_ACK:
269 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
270 case BSSGP_PDUT_BVC_RESET:
271 case BSSGP_PDUT_BVC_RESET_ACK:
272 /* simple case: BVCI IE is mandatory */
273 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
274 goto err_mand_ie;
275 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
276 rc = gbprox_tx2bvci(msg, bvci, ns_bvci);
277 break;
278 case BSSGP_PDUT_PAGING_PS:
279 case BSSGP_PDUT_PAGING_CS:
280 /* process the paging request (LAC/RAC lookup) */
281 rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci);
282 break;
283 case BSSGP_PDUT_STATUS:
284 /* FIXME: Some exception has occurred */
285 LOGP(DGPRS, LOGL_NOTICE, "STATUS not implemented yet\n");
286 break;
287 /* those only exist in the SGSN -> BSS direction */
288 case BSSGP_PDUT_SUSPEND_ACK:
289 case BSSGP_PDUT_SUSPEND_NACK:
290 case BSSGP_PDUT_RESUME_ACK:
291 case BSSGP_PDUT_RESUME_NACK:
292 /* RAC IE is mandatory */
293 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
294 goto err_mand_ie;
295 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
296 if (!peer)
297 goto err_no_peer;
298 rc = gbprox_tx2peer(msg, peer, ns_bvci);
299 break;
300 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
301 LOGP(DGPRS, LOGL_ERROR, "SGSN INVOKE TRACE not supported\n");
302 break;
303 default:
304 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
305 break;
306 }
307
308 return rc;
309err_mand_ie:
310 ; /* FIXME: this would pull gprs_bssgp.c in, which in turn has dependencies */
311 //return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
312err_no_peer:
313 ; /* FIXME */
314}
315
316/* Main input function for Gb proxy */
317int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci)
318{
Harald Weltef01709d2010-05-03 18:54:58 +0200319 int rc;
Harald Welted1991e72010-04-30 20:26:32 +0200320
321 /* Only BVCI=0 messages need special treatment */
322 if (ns_bvci == 0 || ns_bvci == 1) {
323 if (nsvc->remote_end_is_sgsn)
Harald Weltef01709d2010-05-03 18:54:58 +0200324 rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200325 else
Harald Weltef01709d2010-05-03 18:54:58 +0200326 rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
327 } else {
328 /* All other BVCI are PTP and thus can be simply forwarded */
329 if (nsvc->remote_end_is_sgsn) {
330 rc = gbprox_tx2sgsn(msg, ns_bvci);
Harald Weltee49c8292010-05-01 12:10:57 +0200331 } else {
Harald Weltef01709d2010-05-03 18:54:58 +0200332 struct gbprox_peer *peer = peer_by_bvci(ns_bvci);
333 if (!peer) {
334 LOGP(DGPRS, LOGL_NOTICE, "Allocationg new peer for "
335 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
336 nsvc->nsvci, nsvc->nsei);
337 peer = peer_alloc(ns_bvci);
338 peer->nsvc = nsvc;
339 }
340 rc = gbprox_tx2peer(msg, peer, ns_bvci);
Harald Weltee49c8292010-05-01 12:10:57 +0200341 }
Harald Welted1991e72010-04-30 20:26:32 +0200342 }
343
Harald Weltef01709d2010-05-03 18:54:58 +0200344 return rc;
Harald Welted1991e72010-04-30 20:26:32 +0200345}