blob: 3bb0bf94de700e544d3fc019f60e7edf73e14d32 [file] [log] [blame]
Harald Welte24a655f2010-04-30 19:54:29 +02001/* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg);
Harald Welte9ba50052010-03-14 15:45:01 +08002 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
Harald Weltef030b212010-04-26 19:18:54 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24/* Some introduction into NS: NS is used typically on top of frame relay,
25 * but in the ip.access world it is encapsulated in UDP packets. It serves
26 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
27 * do much, apart from providing congestion notification and status indication.
28 *
29 * Terms:
30 * NS Network Service
31 * NSVC NS Virtual Connection
32 * NSEI NS Entity Identifier
33 * NSVL NS Virtual Link
34 * NSVLI NS Virtual Link Identifier
35 * BVC BSSGP Virtual Connection
36 * BVCI BSSGP Virtual Connection Identifier
37 * NSVCG NS Virtual Connection Goup
38 * Blocked NS-VC cannot be used for user traffic
39 * Alive Ability of a NS-VC to provide communication
40 *
41 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
42 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
43 * NS then has to firgure out which NSVC's are responsible for this BVCI.
44 * Those mappings are administratively configured.
45 */
46
47#include <stdlib.h>
48#include <unistd.h>
49#include <errno.h>
50#include <sys/types.h>
51
52#include <arpa/inet.h>
53
54#include <openbsc/gsm_data.h>
55#include <osmocore/msgb.h>
56#include <osmocore/tlv.h>
57#include <osmocore/talloc.h>
Harald Weltef030b212010-04-26 19:18:54 +020058#include <osmocore/select.h>
Harald Welte9ba50052010-03-14 15:45:01 +080059#include <openbsc/debug.h>
60#include <openbsc/gprs_ns.h>
61#include <openbsc/gprs_bssgp.h>
62
63#define NS_ALLOC_SIZE 1024
64
65static const struct tlv_definition ns_att_tlvdef = {
66 .def = {
67 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
68 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
69 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
70 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
71 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
72 },
73};
74
Harald Weltef030b212010-04-26 19:18:54 +020075enum gprs_ns_ll {
76 GPRS_NS_LL_UDP,
77 GPRS_NS_LL_E1,
78};
79
80/* An instance of the NS protocol stack */
81struct gprs_ns_inst {
82 /* callback to the user for incoming UNIT DATA IND */
83 gprs_ns_cb_t *cb;
84
85 /* linked lists of all NSVC in this instance */
86 struct llist_head gprs_nsvcs;
87
88 /* which link-layer are we based on? */
89 enum gprs_ns_ll ll;
90
91 union {
92 /* NS-over-IP specific bits */
93 struct {
94 struct bsc_fd fd;
95 } nsip;
96 };
97};
98
99/* Lookup struct gprs_nsvc based on NSVCI */
100static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi,
101 u_int16_t nsvci)
102{
103 struct gprs_nsvc *nsvc;
104 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
105 if (nsvc->nsvci == nsvci)
106 return nsvc;
107 }
108 return NULL;
109}
110
Harald Welte24a655f2010-04-30 19:54:29 +0200111/* Lookup struct gprs_nsvc based on NSVCI */
112static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi,
113 u_int16_t nsei)
114{
115 struct gprs_nsvc *nsvc;
116 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
117 if (nsvc->nsei == nsei)
118 return nsvc;
119 }
120 return NULL;
121}
122
123
Harald Weltef030b212010-04-26 19:18:54 +0200124/* Lookup struct gprs_nsvc based on remote peer socket addr */
125static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
126 struct sockaddr_in *sin)
127{
128 struct gprs_nsvc *nsvc;
129 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
130 if (!memcmp(&nsvc->ip.bts_addr, sin, sizeof(*sin)))
131 return nsvc;
132 }
133 return NULL;
134}
135
136static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, u_int16_t nsvci)
137{
138 struct gprs_nsvc *nsvc;
139
140 nsvc = talloc_zero(nsi, struct gprs_nsvc);
141 nsvc->nsvci = nsvci;
142 /* before RESET procedure: BLOCKED and DEAD */
143 nsvc->state = NSE_S_BLOCKED;
144 nsvc->nsi = nsi;
145 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
146
147 return nsvc;
148}
Harald Welte9ba50052010-03-14 15:45:01 +0800149
150/* Section 10.3.2, Table 13 */
151static const char *ns_cause_str[] = {
152 [NS_CAUSE_TRANSIT_FAIL] = "Transit network failure",
153 [NS_CAUSE_OM_INTERVENTION] = "O&M intervention",
154 [NS_CAUSE_EQUIP_FAIL] = "Equipment failure",
155 [NS_CAUSE_NSVC_BLOCKED] = "NS-VC blocked",
156 [NS_CAUSE_NSVC_UNKNOWN] = "NS-VC unknown",
157 [NS_CAUSE_BVCI_UNKNOWN] = "BVCI unknown",
158 [NS_CAUSE_SEM_INCORR_PDU] = "Semantically incorrect PDU",
159 [NS_CAUSE_PDU_INCOMP_PSTATE] = "PDU not compatible with protocol state",
160 [NS_CAUSE_PROTO_ERR_UNSPEC] = "Protocol error, unspecified",
161 [NS_CAUSE_INVAL_ESSENT_IE] = "Invalid essential IE",
162 [NS_CAUSE_MISSING_ESSENT_IE] = "Missing essential IE",
163};
164
165static const char *gprs_ns_cause_str(enum ns_cause cause)
166{
167 if (cause >= ARRAY_SIZE(ns_cause_str))
168 return "undefined";
169
170 if (ns_cause_str[cause])
171 return ns_cause_str[cause];
172
173 return "undefined";
174}
175
Harald Welte24a655f2010-04-30 19:54:29 +0200176static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200177
Harald Welte24a655f2010-04-30 19:54:29 +0200178static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800179{
Harald Weltef030b212010-04-26 19:18:54 +0200180 int ret;
181
182 switch (nsvc->nsi->ll) {
183 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200184 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200185 break;
186 default:
187 LOGP(DGPRS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
188 msgb_free(msg);
189 ret = -EIO;
190 break;
191 }
192 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800193}
194
Harald Weltec5478482010-03-18 00:01:43 +0800195static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, u_int8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800196{
197 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
198 struct gprs_ns_hdr *nsh;
199
200 if (!msg)
201 return -ENOMEM;
202
203 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
204
205 nsh->pdu_type = pdu_type;
206
Harald Welte24a655f2010-04-30 19:54:29 +0200207 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800208}
209
210#define NS_TIMER_ALIVE 3, 0 /* after 3 seconds without response, we retry */
211#define NS_TIMER_TEST 30, 0 /* every 10 seconds we check if the BTS is still alive */
212#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
213
214static void gprs_ns_alive_cb(void *data)
215{
216 struct gprs_nsvc *nsvc = data;
217
218 if (nsvc->timer_is_tns_alive) {
219 /* Tns-alive case: we expired without response ! */
220 nsvc->alive_retries++;
221 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
222 /* mark as dead and blocked */
223 nsvc->state = NSE_S_BLOCKED;
224 DEBUGP(DGPRS, "Tns-alive more then %u retries, "
225 " blocking NS-VC\n", NS_ALIVE_RETRIES);
226 /* FIXME: inform higher layers */
227 return;
228 }
229 } else {
230 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltec5478482010-03-18 00:01:43 +0800231 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800232 /* start Tns-alive timer */
233 nsvc->timer_is_tns_alive = 1;
234 }
235 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
236}
237
238/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800239static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800240{
241 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
242 struct gprs_ns_hdr *nsh;
Harald Weltec5478482010-03-18 00:01:43 +0800243 u_int16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800244
245 if (!msg)
246 return -ENOMEM;
247
Harald Weltec5478482010-03-18 00:01:43 +0800248 nsvci = htons(nsvc->nsvci);
249 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800250
251 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
252
253 nsh->pdu_type = NS_PDUT_RESET_ACK;
254
Harald Weltec5478482010-03-18 00:01:43 +0800255 DEBUGP(DGPRS, "nsvci=%u, nsei=%u\n", nsvc->nsvci, nsvc->nsei);
256
Harald Welte9ba50052010-03-14 15:45:01 +0800257 msgb_tvlv_put(msg, NS_IE_VCI, 2, (u_int8_t *)&nsvci);
258 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (u_int8_t *)&nsei);
259
Harald Welte24a655f2010-04-30 19:54:29 +0200260 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800261}
262
Harald Welte24a655f2010-04-30 19:54:29 +0200263/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
264int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800265{
Harald Welte24a655f2010-04-30 19:54:29 +0200266 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800267 struct gprs_ns_hdr *nsh;
Harald Welte24a655f2010-04-30 19:54:29 +0200268 u_int16_t bvci = msgb_bvci(msg);
269
270 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
271 if (!nsvc) {
272 DEBUGP(DGPRS, "Unable to resolve NSEI %u to NS-VC!\n", msgb_nsei(msg));
273 return -EINVAL;
274 }
Harald Welte9ba50052010-03-14 15:45:01 +0800275
276 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
277 if (!nsh) {
278 DEBUGP(DGPRS, "Not enough headroom for NS header\n");
279 return -EIO;
280 }
281
282 nsh->pdu_type = NS_PDUT_UNITDATA;
283 /* spare octet in data[0] */
284 nsh->data[1] = bvci >> 8;
285 nsh->data[2] = bvci & 0xff;
286
Harald Welte24a655f2010-04-30 19:54:29 +0200287 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800288}
289
290/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200291static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800292{
293 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
294 u_int16_t bvci;
295
296 /* spare octet in data[0] */
297 bvci = nsh->data[1] << 8 | nsh->data[2];
298 msg->l3h = &nsh->data[3];
299
300 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200301 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800302}
303
304/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200305static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800306{
Harald Weltef030b212010-04-26 19:18:54 +0200307 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800308 struct tlv_parsed tp;
309 u_int8_t cause;
310 int rc;
311
312 DEBUGP(DGPRS, "NS STATUS ");
313
314 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
315
316 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
317 DEBUGPC(DGPRS, "missing cause IE\n");
318 return -EINVAL;
319 }
320
321 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
322 DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause));
323
324 return 0;
325}
326
327/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200328static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800329{
330 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800331 struct tlv_parsed tp;
332 u_int8_t *cause;
333 u_int16_t *nsvci, *nsei;
334 int rc;
335
336 DEBUGP(DGPRS, "NS RESET ");
337
338 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
339
340 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
341 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
342 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
343 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
344 DEBUGPC(DGPRS, "Missing mandatory IE\n");
345 return -EINVAL;
346 }
347
348 cause = (u_int8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
349 nsvci = (u_int16_t *) TLVP_VAL(&tp, NS_IE_VCI);
350 nsei = (u_int16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
351
Harald Weltec5478482010-03-18 00:01:43 +0800352 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
353 nsvc->nsei = ntohs(*nsei);
354 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800355
356 DEBUGPC(DGPRS, "cause=%s, NSVCI=%u, NSEI=%u\n",
Harald Weltec5478482010-03-18 00:01:43 +0800357 gprs_ns_cause_str(*cause), nsvc->nsvci, nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800358
359 /* mark the NS-VC as blocked and alive */
Harald Welte9ba50052010-03-14 15:45:01 +0800360 /* start the test procedure */
361 nsvc->alive_timer.cb = gprs_ns_alive_cb;
362 nsvc->alive_timer.data = nsvc;
363 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
364
Harald Weltec5478482010-03-18 00:01:43 +0800365 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800366}
367
368/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200369int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
370 struct sockaddr_in *saddr)
Harald Welte9ba50052010-03-14 15:45:01 +0800371{
372 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200373 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800374 int rc = 0;
375
Harald Weltef030b212010-04-26 19:18:54 +0200376 /* look up the NSVC based on source address */
377 nsvc = nsvc_by_rem_addr(nsi, saddr);
378 if (!nsvc) {
379 /* Only the RESET procedure creates a new NSVC */
380 if (nsh->pdu_type != NS_PDUT_RESET)
381 return -EIO;
382 nsvc = nsvc_create(nsi, 0xffff);
383 nsvc->ip.bts_addr = *saddr;
Harald Welte24a655f2010-04-30 19:54:29 +0200384 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200385 return rc;
386 }
Harald Welte24a655f2010-04-30 19:54:29 +0200387 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800388
Harald Welte9ba50052010-03-14 15:45:01 +0800389 switch (nsh->pdu_type) {
390 case NS_PDUT_ALIVE:
391 /* remote end inquires whether we're still alive,
392 * we need to respond with ALIVE_ACK */
Harald Weltec5478482010-03-18 00:01:43 +0800393 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800394 break;
395 case NS_PDUT_ALIVE_ACK:
396 /* stop Tns-alive */
397 bsc_del_timer(&nsvc->alive_timer);
398 /* start Tns-test */
399 nsvc->timer_is_tns_alive = 0;
400 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_TEST);
401 break;
402 case NS_PDUT_UNITDATA:
403 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200404 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800405 break;
406 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200407 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800408 break;
409 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200410 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800411 break;
412 case NS_PDUT_RESET_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200413 DEBUGP(DGPRS, "NS RESET ACK\n");
414 /* mark remote NS-VC as blocked + active */
415 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800416 break;
417 case NS_PDUT_UNBLOCK:
418 /* Section 7.2: unblocking procedure */
419 DEBUGP(DGPRS, "NS UNBLOCK\n");
420 nsvc->state &= ~NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800421 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800422 break;
423 case NS_PDUT_UNBLOCK_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200424 DEBUGP(DGPRS, "NS UNBLOCK ACK\n");
425 /* mark remote NS-VC as unblocked + active */
426 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800427 break;
428 case NS_PDUT_BLOCK:
429 DEBUGP(DGPRS, "NS BLOCK\n");
430 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800431 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800432 break;
433 case NS_PDUT_BLOCK_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200434 DEBUGP(DGPRS, "NS BLOCK ACK\n");
435 /* mark remote NS-VC as blocked + active */
436 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800437 break;
438 default:
439 DEBUGP(DGPRS, "Unknown NS PDU type 0x%02x\n", nsh->pdu_type);
440 rc = -EINVAL;
441 break;
442 }
443 return rc;
444}
445
Harald Weltef030b212010-04-26 19:18:54 +0200446struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
447{
448 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
449
450 nsi->cb = cb;
451 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
452
Harald Welte3771d092010-04-30 20:26:32 +0200453 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200454}
455
456void gprs_ns_destroy(struct gprs_ns_inst *nsi)
457{
458 /* FIXME: clear all timers */
459
460 /* recursively free the NSI and all its NSVCs */
461 talloc_free(nsi);
462}
463
464
465/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
466 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
467
468/* Read a single NS-over-IP message */
469static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
470 struct sockaddr_in *saddr)
471{
472 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
473 int ret = 0;
474 socklen_t saddr_len = sizeof(*saddr);
475
476 if (!msg) {
477 *error = -ENOMEM;
478 return NULL;
479 }
480
481 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
482 (struct sockaddr *)saddr, &saddr_len);
483 if (ret < 0) {
484 fprintf(stderr, "recv error %s\n", strerror(errno));
485 msgb_free(msg);
486 *error = ret;
487 return NULL;
488 } else if (ret == 0) {
489 msgb_free(msg);
490 *error = ret;
491 return NULL;
492 }
493
494 msg->l2h = msg->data;
495 msgb_put(msg, ret);
496
497 return msg;
498}
499
500static int handle_nsip_read(struct bsc_fd *bfd)
501{
502 int error;
503 struct sockaddr_in saddr;
504 struct gprs_ns_inst *nsi = bfd->data;
505 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
506
507 if (!msg)
508 return error;
509
510 return gprs_ns_rcvmsg(nsi, msg, &saddr);
511}
512
513static int handle_nsip_write(struct bsc_fd *bfd)
514{
515 /* FIXME: actually send the data here instead of nsip_sendmsg() */
516 return -EIO;
517}
518
Harald Welte24a655f2010-04-30 19:54:29 +0200519int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200520{
521 int rc;
522 struct gprs_ns_inst *nsi = nsvc->nsi;
523 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
524
525 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
526 (struct sockaddr *)daddr, sizeof(*daddr));
527
528 talloc_free(msg);
529
530 return rc;
531}
532
533/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
534static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
535{
536 int rc = 0;
537
538 if (what & BSC_FD_READ)
539 rc = handle_nsip_read(bfd);
540 if (what & BSC_FD_WRITE)
541 rc = handle_nsip_write(bfd);
542
543 return rc;
544}
545
546
547/* FIXME: this is currently in input/ipaccess.c */
548extern int make_sock(struct bsc_fd *bfd, int proto, u_int16_t port,
549 int (*cb)(struct bsc_fd *fd, unsigned int what));
550
551/* Listen for incoming GPRS packets */
552int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
553{
554 int ret;
555
556 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
557 if (ret < 0)
558 return ret;
559
560 nsi->ll = GPRS_NS_LL_UDP;
561 nsi->nsip.fd.data = nsi;
562
563 return ret;
564}
Harald Welte3771d092010-04-30 20:26:32 +0200565
566/* Establish a connection (from the BSS) to the SGSN */
567struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
568 struct sockaddr_in *dest, uint16_t nsvci)
569{
570 struct gprs_nsvc *nsvc;
571
572 nsvc = nsvc_by_rem_addr(nsi, dest);
573 if (!nsvc) {
574 nsvc = nsvc_create(nsi, nsvci);
575 nsvc->ip.bts_addr = *dest;
576 }
577 nsvc->remote_end_is_sgsn = 1;
578
579 /* Initiate a RESET procedure */
580 if (gprs_ns_tx_simple(nsvc, NS_PDUT_RESET) < 0)
581 return NULL;
582 /* FIXME: should we run a timer and re-transmit the reset request? */
583
584 return nsvc;
585}