blob: c737a6dbb4063a6a5fe15864562de454f0b27fd5 [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
Harald Welte3ad32432010-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 Welted1991e72010-04-30 20:26:32 +0200130/* feed a message down the NS-VC associated with the specified peer */
Harald Welte3ad32432010-05-03 19:05:10 +0200131static int gbprox_relay2sgsn(struct msgb *msg, uint16_t ns_bvci)
Harald Weltef01709d2010-05-03 18:54:58 +0200132{
133 msgb_bvci(msg) = ns_bvci;
134 msgb_nsei(msg) = gbcfg.nsip_sgsn_nsei;
135
136 DEBUGP(DGPRS, "proxying message to SGSN (NS_BVCI=%u, NSEI=%u)\n",
137 ns_bvci, gbcfg.nsip_sgsn_nsei);
138
Harald Welte3ad32432010-05-03 19:05:10 +0200139 strip_ns_hdr(msg);
140
Harald Weltef01709d2010-05-03 18:54:58 +0200141 return gprs_ns_sendmsg(gbprox_nsi, msg);
142}
143
Harald Weltef01709d2010-05-03 18:54:58 +0200144/* feed a message down the NS-VC associated with the specified peer */
Harald Welte3ad32432010-05-03 19:05:10 +0200145static int gbprox_relay2peer(struct msgb *msg, struct gbprox_peer *peer,
Harald Welted1991e72010-04-30 20:26:32 +0200146 uint16_t ns_bvci)
147{
148 msgb_bvci(msg) = ns_bvci;
149 msgb_nsei(msg) = peer->nsvc->nsei;
150
Harald Weltef01709d2010-05-03 18:54:58 +0200151 DEBUGP(DGPRS, "proxying message to BSS (NS_BVCI=%u, NSEI=%u)\n",
152 ns_bvci, peer->nsvc->nsei);
153
Harald Welte3ad32432010-05-03 19:05:10 +0200154 strip_ns_hdr(msg);
155
Harald Welted1991e72010-04-30 20:26:32 +0200156 return gprs_ns_sendmsg(gbprox_nsi, msg);
157}
158
159/* Send a message to a peer identified by ptp_bvci but using ns_bvci
160 * in the NS hdr */
Harald Welte3ad32432010-05-03 19:05:10 +0200161static int gbprox_relay2bvci(struct msgb *msg, uint16_t ptp_bvci,
Harald Welted1991e72010-04-30 20:26:32 +0200162 uint16_t ns_bvci)
163{
164 struct gbprox_peer *peer;
165
166 peer = peer_by_bvci(ptp_bvci);
167 if (!peer)
168 return -ENOENT;
169
Harald Welte3ad32432010-05-03 19:05:10 +0200170 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200171}
172
173/* Receive an incoming signalling message from a BSS-side NS-VC */
174static int gbprox_rx_sig_from_bss(struct msgb *msg, struct gprs_nsvc *nsvc,
175 uint16_t ns_bvci)
176{
Harald Welte2ac2e912010-05-03 16:30:59 +0200177 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200178 struct tlv_parsed tp;
179 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200180 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +0200181 struct gbprox_peer *from_peer;
Harald Weltee49c8292010-05-01 12:10:57 +0200182 struct gprs_ra_id raid;
Harald Welted1991e72010-04-30 20:26:32 +0200183
184 if (ns_bvci != 0) {
Harald Weltee49c8292010-05-01 12:10:57 +0200185 LOGP(DGPRS, LOGL_NOTICE, "BVCI %u is not signalling\n", ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200186 return -EINVAL;
187 }
188
189 /* we actually should never see those two for BVCI == 0, but double-check
190 * just to make sure */
191 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
192 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
193 LOGP(DGPRS, LOGL_NOTICE, "UNITDATA not allowed in signalling\n");
194 return -EINVAL;
195 }
196
197 bssgp_tlv_parse(&tp, bgph->data, data_len);
198
199 switch (pdu_type) {
200 case BSSGP_PDUT_SUSPEND:
201 case BSSGP_PDUT_RESUME:
Harald Weltee49c8292010-05-01 12:10:57 +0200202 /* We implement RAC snooping during SUSPEND/RESUME, since
203 * it establishes a relationsip between BVCI/peer and the
204 * routeing area code. The snooped information is then
205 * used for routing the {SUSPEND,RESUME}_[N]ACK back to
206 * the correct BSSGP */
Harald Welted1991e72010-04-30 20:26:32 +0200207 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
208 goto err_mand_ie;
209 from_peer = peer_by_nsvc(nsvc);
210 if (!from_peer)
211 goto err_no_peer;
212 memcpy(&from_peer->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
213 sizeof(&from_peer->ra));
Harald Weltee49c8292010-05-01 12:10:57 +0200214 gsm48_parse_ra(&raid, &from_peer->ra);
215 DEBUGP(DGPRS, "RAC snooping: RAC %u/%u/%u/%u behind BVCI=%u, "
216 "NSVCI=%u, NSEI=%u\n", raid.mcc, raid.mnc, raid.lac,
217 raid.rac , from_peer->bvci, nsvc->nsvci, nsvc->nsei);
Harald Welted1991e72010-04-30 20:26:32 +0200218 /* FIXME: This only supports one BSS per RA */
219 break;
220 }
221
222 /* Normally, we can simply pass on all signalling messages from BSS to SGSN */
Harald Welte3ad32432010-05-03 19:05:10 +0200223 return gbprox_relay2sgsn(msg, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200224err_no_peer:
225err_mand_ie:
226 /* FIXME: do something */
227 ;
228}
229
230/* Receive paging request from SGSN, we need to relay to proper BSS */
231static int gbprox_rx_paging(struct msgb *msg, struct tlv_parsed *tp,
232 struct gprs_nsvc *nsvc, uint16_t ns_bvci)
233{
234 struct gbprox_peer *peer;
235
236 if (TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
237 uint16_t bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte3ad32432010-05-03 19:05:10 +0200238 return gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200239 } else if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
240 peer = peer_by_rac(TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte3ad32432010-05-03 19:05:10 +0200241 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200242 } else if (TLVP_PRESENT(tp, BSSGP_IE_LOCATION_AREA)) {
243 peer = peer_by_lac(TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA));
Harald Welte3ad32432010-05-03 19:05:10 +0200244 return gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200245 } else
246 return -EINVAL;
247}
248
249/* Receive an incoming signalling message from the SGSN-side NS-VC */
250static int gbprox_rx_sig_from_sgsn(struct msgb *msg, struct gprs_nsvc *nsvc,
251 uint16_t ns_bvci)
252{
Harald Welte2ac2e912010-05-03 16:30:59 +0200253 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welted1991e72010-04-30 20:26:32 +0200254 struct tlv_parsed tp;
255 uint8_t pdu_type = bgph->pdu_type;
Harald Welte2ac2e912010-05-03 16:30:59 +0200256 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welted1991e72010-04-30 20:26:32 +0200257 struct gbprox_peer *peer;
258 uint16_t bvci;
259 int rc = 0;
260
261 if (ns_bvci != 0) {
Harald Weltee49c8292010-05-01 12:10:57 +0200262 LOGP(DGPRS, LOGL_NOTICE, "BVCI %u is not signalling\n", ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200263 return -EINVAL;
264 }
265
266 /* we actually should never see those two for BVCI == 0, but double-check
267 * just to make sure */
268 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
269 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
270 LOGP(DGPRS, LOGL_NOTICE, "UNITDATA not allowed in signalling\n");
271 return -EINVAL;
272 }
273
274 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
275
276 switch (pdu_type) {
277 case BSSGP_PDUT_FLUSH_LL:
278 case BSSGP_PDUT_BVC_BLOCK_ACK:
279 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
280 case BSSGP_PDUT_BVC_RESET:
281 case BSSGP_PDUT_BVC_RESET_ACK:
282 /* simple case: BVCI IE is mandatory */
283 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
284 goto err_mand_ie;
285 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welte3ad32432010-05-03 19:05:10 +0200286 rc = gbprox_relay2bvci(msg, bvci, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200287 break;
288 case BSSGP_PDUT_PAGING_PS:
289 case BSSGP_PDUT_PAGING_CS:
290 /* process the paging request (LAC/RAC lookup) */
291 rc = gbprox_rx_paging(msg, &tp, nsvc, ns_bvci);
292 break;
293 case BSSGP_PDUT_STATUS:
294 /* FIXME: Some exception has occurred */
295 LOGP(DGPRS, LOGL_NOTICE, "STATUS not implemented yet\n");
296 break;
297 /* those only exist in the SGSN -> BSS direction */
298 case BSSGP_PDUT_SUSPEND_ACK:
299 case BSSGP_PDUT_SUSPEND_NACK:
300 case BSSGP_PDUT_RESUME_ACK:
301 case BSSGP_PDUT_RESUME_NACK:
302 /* RAC IE is mandatory */
303 if (!TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
304 goto err_mand_ie;
305 peer = peer_by_rac(TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
306 if (!peer)
307 goto err_no_peer;
Harald Welte3ad32432010-05-03 19:05:10 +0200308 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200309 break;
310 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
311 LOGP(DGPRS, LOGL_ERROR, "SGSN INVOKE TRACE not supported\n");
312 break;
313 default:
314 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
315 break;
316 }
317
318 return rc;
319err_mand_ie:
320 ; /* FIXME: this would pull gprs_bssgp.c in, which in turn has dependencies */
321 //return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
322err_no_peer:
323 ; /* FIXME */
324}
325
326/* Main input function for Gb proxy */
327int gbprox_rcvmsg(struct msgb *msg, struct gprs_nsvc *nsvc, uint16_t ns_bvci)
328{
Harald Weltef01709d2010-05-03 18:54:58 +0200329 int rc;
Harald Welted1991e72010-04-30 20:26:32 +0200330
331 /* Only BVCI=0 messages need special treatment */
332 if (ns_bvci == 0 || ns_bvci == 1) {
333 if (nsvc->remote_end_is_sgsn)
Harald Weltef01709d2010-05-03 18:54:58 +0200334 rc = gbprox_rx_sig_from_sgsn(msg, nsvc, ns_bvci);
Harald Welted1991e72010-04-30 20:26:32 +0200335 else
Harald Weltef01709d2010-05-03 18:54:58 +0200336 rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
337 } else {
338 /* All other BVCI are PTP and thus can be simply forwarded */
339 if (nsvc->remote_end_is_sgsn) {
Harald Welte3ad32432010-05-03 19:05:10 +0200340 rc = gbprox_relay2sgsn(msg, ns_bvci);
Harald Weltee49c8292010-05-01 12:10:57 +0200341 } else {
Harald Weltef01709d2010-05-03 18:54:58 +0200342 struct gbprox_peer *peer = peer_by_bvci(ns_bvci);
343 if (!peer) {
344 LOGP(DGPRS, LOGL_NOTICE, "Allocationg new peer for "
345 "BVCI=%u via NSVC=%u/NSEI=%u\n", ns_bvci,
346 nsvc->nsvci, nsvc->nsei);
347 peer = peer_alloc(ns_bvci);
348 peer->nsvc = nsvc;
349 }
Harald Welte3ad32432010-05-03 19:05:10 +0200350 rc = gbprox_relay2peer(msg, peer, ns_bvci);
Harald Weltee49c8292010-05-01 12:10:57 +0200351 }
Harald Welted1991e72010-04-30 20:26:32 +0200352 }
353
Harald Weltef01709d2010-05-03 18:54:58 +0200354 return rc;
Harald Welted1991e72010-04-30 20:26:32 +0200355}