blob: b57de1d726369569294a0278efdce33d1e1803c3 [file] [log] [blame]
Harald Welte75bb8202010-03-14 15:45:01 +08001/* GPRS Networks Service (NS) messages on the Gb interface
2 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
Harald Weltef67a5f92010-04-26 19:18:54 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte75bb8202010-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 Weltef67a5f92010-04-26 19:18:54 +020058#include <osmocore/select.h>
Harald Welte75bb8202010-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
75#define NSE_S_BLOCKED 0x0001
76#define NSE_S_ALIVE 0x0002
77
78struct gprs_nsvc {
79 struct llist_head list;
Harald Weltef67a5f92010-04-26 19:18:54 +020080 struct gprs_ns_inst *nsi;
Harald Welte75bb8202010-03-14 15:45:01 +080081
82 u_int16_t nsei; /* end-to-end significance */
83 u_int16_t nsvci; /* uniquely identifies NS-VC at SGSN */
84
85 u_int32_t state;
Harald Weltef67a5f92010-04-26 19:18:54 +020086 u_int32_t remote_state;
Harald Welte75bb8202010-03-14 15:45:01 +080087
88 struct timer_list alive_timer;
89 int timer_is_tns_alive;
90 int alive_retries;
Harald Welte7491c772010-03-18 00:01:43 +080091
92 union {
93 struct {
94 struct sockaddr_in bts_addr;
95 } ip;
96 };
Harald Welte75bb8202010-03-14 15:45:01 +080097};
98
Harald Weltef67a5f92010-04-26 19:18:54 +020099enum gprs_ns_ll {
100 GPRS_NS_LL_UDP,
101 GPRS_NS_LL_E1,
102};
103
104/* An instance of the NS protocol stack */
105struct gprs_ns_inst {
106 /* callback to the user for incoming UNIT DATA IND */
107 gprs_ns_cb_t *cb;
108
109 /* linked lists of all NSVC in this instance */
110 struct llist_head gprs_nsvcs;
111
112 /* which link-layer are we based on? */
113 enum gprs_ns_ll ll;
114
115 union {
116 /* NS-over-IP specific bits */
117 struct {
118 struct bsc_fd fd;
119 } nsip;
120 };
121};
122
123/* Lookup struct gprs_nsvc based on NSVCI */
124static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi,
125 u_int16_t nsvci)
126{
127 struct gprs_nsvc *nsvc;
128 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
129 if (nsvc->nsvci == nsvci)
130 return nsvc;
131 }
132 return NULL;
133}
134
135/* Lookup struct gprs_nsvc based on remote peer socket addr */
136static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
137 struct sockaddr_in *sin)
138{
139 struct gprs_nsvc *nsvc;
140 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
141 if (!memcmp(&nsvc->ip.bts_addr, sin, sizeof(*sin)))
142 return nsvc;
143 }
144 return NULL;
145}
146
147static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, u_int16_t nsvci)
148{
149 struct gprs_nsvc *nsvc;
150
151 nsvc = talloc_zero(nsi, struct gprs_nsvc);
152 nsvc->nsvci = nsvci;
153 /* before RESET procedure: BLOCKED and DEAD */
154 nsvc->state = NSE_S_BLOCKED;
155 nsvc->nsi = nsi;
156 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
157
158 return nsvc;
159}
Harald Welte75bb8202010-03-14 15:45:01 +0800160
161/* Section 10.3.2, Table 13 */
162static const char *ns_cause_str[] = {
163 [NS_CAUSE_TRANSIT_FAIL] = "Transit network failure",
164 [NS_CAUSE_OM_INTERVENTION] = "O&M intervention",
165 [NS_CAUSE_EQUIP_FAIL] = "Equipment failure",
166 [NS_CAUSE_NSVC_BLOCKED] = "NS-VC blocked",
167 [NS_CAUSE_NSVC_UNKNOWN] = "NS-VC unknown",
168 [NS_CAUSE_BVCI_UNKNOWN] = "BVCI unknown",
169 [NS_CAUSE_SEM_INCORR_PDU] = "Semantically incorrect PDU",
170 [NS_CAUSE_PDU_INCOMP_PSTATE] = "PDU not compatible with protocol state",
171 [NS_CAUSE_PROTO_ERR_UNSPEC] = "Protocol error, unspecified",
172 [NS_CAUSE_INVAL_ESSENT_IE] = "Invalid essential IE",
173 [NS_CAUSE_MISSING_ESSENT_IE] = "Missing essential IE",
174};
175
176static const char *gprs_ns_cause_str(enum ns_cause cause)
177{
178 if (cause >= ARRAY_SIZE(ns_cause_str))
179 return "undefined";
180
181 if (ns_cause_str[cause])
182 return ns_cause_str[cause];
183
184 return "undefined";
185}
186
Harald Weltef67a5f92010-04-26 19:18:54 +0200187static int nsip_sendmsg(struct msgb *msg, struct gprs_nsvc *nsvc);
188
Harald Welte7491c772010-03-18 00:01:43 +0800189static int gprs_ns_tx(struct msgb *msg, struct gprs_nsvc *nsvc)
Harald Welte75bb8202010-03-14 15:45:01 +0800190{
Harald Weltef67a5f92010-04-26 19:18:54 +0200191 int ret;
192
193 switch (nsvc->nsi->ll) {
194 case GPRS_NS_LL_UDP:
195 ret = nsip_sendmsg(msg, nsvc);
196 break;
197 default:
198 LOGP(DGPRS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
199 msgb_free(msg);
200 ret = -EIO;
201 break;
202 }
203 return ret;
Harald Welte75bb8202010-03-14 15:45:01 +0800204}
205
Harald Welte7491c772010-03-18 00:01:43 +0800206static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, u_int8_t pdu_type)
Harald Welte75bb8202010-03-14 15:45:01 +0800207{
208 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
209 struct gprs_ns_hdr *nsh;
210
211 if (!msg)
212 return -ENOMEM;
213
214 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
215
216 nsh->pdu_type = pdu_type;
217
Harald Welte7491c772010-03-18 00:01:43 +0800218 return gprs_ns_tx(msg, nsvc);
Harald Welte75bb8202010-03-14 15:45:01 +0800219}
220
221#define NS_TIMER_ALIVE 3, 0 /* after 3 seconds without response, we retry */
222#define NS_TIMER_TEST 30, 0 /* every 10 seconds we check if the BTS is still alive */
223#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
224
225static void gprs_ns_alive_cb(void *data)
226{
227 struct gprs_nsvc *nsvc = data;
228
229 if (nsvc->timer_is_tns_alive) {
230 /* Tns-alive case: we expired without response ! */
231 nsvc->alive_retries++;
232 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
233 /* mark as dead and blocked */
234 nsvc->state = NSE_S_BLOCKED;
235 DEBUGP(DGPRS, "Tns-alive more then %u retries, "
236 " blocking NS-VC\n", NS_ALIVE_RETRIES);
237 /* FIXME: inform higher layers */
238 return;
239 }
240 } else {
241 /* Tns-test case: send NS-ALIVE PDU */
Harald Welte7491c772010-03-18 00:01:43 +0800242 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Welte75bb8202010-03-14 15:45:01 +0800243 /* start Tns-alive timer */
244 nsvc->timer_is_tns_alive = 1;
245 }
246 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
247}
248
249/* Section 9.2.6 */
Harald Welte7491c772010-03-18 00:01:43 +0800250static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte75bb8202010-03-14 15:45:01 +0800251{
252 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
253 struct gprs_ns_hdr *nsh;
Harald Welte7491c772010-03-18 00:01:43 +0800254 u_int16_t nsvci, nsei;
Harald Welte75bb8202010-03-14 15:45:01 +0800255
256 if (!msg)
257 return -ENOMEM;
258
Harald Welte7491c772010-03-18 00:01:43 +0800259 nsvci = htons(nsvc->nsvci);
260 nsei = htons(nsvc->nsei);
Harald Welte75bb8202010-03-14 15:45:01 +0800261
262 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
263
264 nsh->pdu_type = NS_PDUT_RESET_ACK;
265
Harald Welte7491c772010-03-18 00:01:43 +0800266 DEBUGP(DGPRS, "nsvci=%u, nsei=%u\n", nsvc->nsvci, nsvc->nsei);
267
Harald Welte75bb8202010-03-14 15:45:01 +0800268 msgb_tvlv_put(msg, NS_IE_VCI, 2, (u_int8_t *)&nsvci);
269 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (u_int8_t *)&nsei);
270
Harald Welte7491c772010-03-18 00:01:43 +0800271 return gprs_ns_tx(msg, nsvc);
Harald Welte75bb8202010-03-14 15:45:01 +0800272}
273
274/* Section 9.2.10: transmit side */
Harald Weltef67a5f92010-04-26 19:18:54 +0200275int gprs_ns_sendmsg(struct gprs_nsvc *nsvc, u_int16_t bvci,
Harald Welte75bb8202010-03-14 15:45:01 +0800276 struct msgb *msg)
277{
278 struct gprs_ns_hdr *nsh;
279
280 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
281 if (!nsh) {
282 DEBUGP(DGPRS, "Not enough headroom for NS header\n");
283 return -EIO;
284 }
285
286 nsh->pdu_type = NS_PDUT_UNITDATA;
287 /* spare octet in data[0] */
288 nsh->data[1] = bvci >> 8;
289 nsh->data[2] = bvci & 0xff;
290
Harald Welte7491c772010-03-18 00:01:43 +0800291 return gprs_ns_tx(msg, nsvc);
Harald Welte75bb8202010-03-14 15:45:01 +0800292}
293
294/* Section 9.2.10: receive side */
Harald Welte332347e2010-04-30 16:43:39 +0200295static int gprs_ns_rx_unitdata(struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +0800296{
297 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte332347e2010-04-30 16:43:39 +0200298 struct gprs_nsvc *nsvc = msgb_nsvc(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800299 u_int16_t bvci;
300
301 /* spare octet in data[0] */
302 bvci = nsh->data[1] << 8 | nsh->data[2];
303 msg->l3h = &nsh->data[3];
304
305 /* call upper layer (BSSGP) */
Harald Weltef67a5f92010-04-26 19:18:54 +0200306 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte75bb8202010-03-14 15:45:01 +0800307}
308
309/* Section 9.2.7 */
Harald Welte332347e2010-04-30 16:43:39 +0200310static int gprs_ns_rx_status(struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +0800311{
Harald Weltef67a5f92010-04-26 19:18:54 +0200312 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte332347e2010-04-30 16:43:39 +0200313 struct gprs_nsvc *nsvc = msgb_nsvc(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800314 struct tlv_parsed tp;
315 u_int8_t cause;
316 int rc;
317
318 DEBUGP(DGPRS, "NS STATUS ");
319
320 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
321
322 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
323 DEBUGPC(DGPRS, "missing cause IE\n");
324 return -EINVAL;
325 }
326
327 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
328 DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause));
329
330 return 0;
331}
332
333/* Section 7.3 */
Harald Welte332347e2010-04-30 16:43:39 +0200334static int gprs_ns_rx_reset(struct msgb *msg)
Harald Welte75bb8202010-03-14 15:45:01 +0800335{
336 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte332347e2010-04-30 16:43:39 +0200337 struct gprs_nsvc *nsvc = msgb_nsvc(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800338 struct tlv_parsed tp;
339 u_int8_t *cause;
340 u_int16_t *nsvci, *nsei;
341 int rc;
342
343 DEBUGP(DGPRS, "NS RESET ");
344
345 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
346
347 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
348 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
349 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
350 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
351 DEBUGPC(DGPRS, "Missing mandatory IE\n");
352 return -EINVAL;
353 }
354
355 cause = (u_int8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
356 nsvci = (u_int16_t *) TLVP_VAL(&tp, NS_IE_VCI);
357 nsei = (u_int16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
358
Harald Welte7491c772010-03-18 00:01:43 +0800359 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
360 nsvc->nsei = ntohs(*nsei);
361 nsvc->nsvci = ntohs(*nsvci);
Harald Welte75bb8202010-03-14 15:45:01 +0800362
363 DEBUGPC(DGPRS, "cause=%s, NSVCI=%u, NSEI=%u\n",
Harald Welte7491c772010-03-18 00:01:43 +0800364 gprs_ns_cause_str(*cause), nsvc->nsvci, nsvc->nsei);
Harald Welte75bb8202010-03-14 15:45:01 +0800365
366 /* mark the NS-VC as blocked and alive */
Harald Welte75bb8202010-03-14 15:45:01 +0800367 /* start the test procedure */
368 nsvc->alive_timer.cb = gprs_ns_alive_cb;
369 nsvc->alive_timer.data = nsvc;
370 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
371
Harald Welte7491c772010-03-18 00:01:43 +0800372 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte75bb8202010-03-14 15:45:01 +0800373}
374
375/* main entry point, here incoming NS frames enter */
Harald Weltef67a5f92010-04-26 19:18:54 +0200376int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
377 struct sockaddr_in *saddr)
Harald Welte75bb8202010-03-14 15:45:01 +0800378{
379 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef67a5f92010-04-26 19:18:54 +0200380 struct gprs_nsvc *nsvc;
Harald Welte75bb8202010-03-14 15:45:01 +0800381 int rc = 0;
382
Harald Weltef67a5f92010-04-26 19:18:54 +0200383 /* look up the NSVC based on source address */
384 nsvc = nsvc_by_rem_addr(nsi, saddr);
385 if (!nsvc) {
386 /* Only the RESET procedure creates a new NSVC */
387 if (nsh->pdu_type != NS_PDUT_RESET)
388 return -EIO;
389 nsvc = nsvc_create(nsi, 0xffff);
390 nsvc->ip.bts_addr = *saddr;
Harald Welte332347e2010-04-30 16:43:39 +0200391 rc = gprs_ns_rx_reset(msg);
Harald Weltef67a5f92010-04-26 19:18:54 +0200392 return rc;
393 }
Harald Welte332347e2010-04-30 16:43:39 +0200394 msgb_nsvc(msg) = nsvc;
Harald Welte7491c772010-03-18 00:01:43 +0800395
Harald Welte75bb8202010-03-14 15:45:01 +0800396 switch (nsh->pdu_type) {
397 case NS_PDUT_ALIVE:
398 /* remote end inquires whether we're still alive,
399 * we need to respond with ALIVE_ACK */
Harald Welte7491c772010-03-18 00:01:43 +0800400 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
Harald Welte75bb8202010-03-14 15:45:01 +0800401 break;
402 case NS_PDUT_ALIVE_ACK:
403 /* stop Tns-alive */
404 bsc_del_timer(&nsvc->alive_timer);
405 /* start Tns-test */
406 nsvc->timer_is_tns_alive = 0;
407 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_TEST);
408 break;
409 case NS_PDUT_UNITDATA:
410 /* actual user data */
Harald Welte332347e2010-04-30 16:43:39 +0200411 rc = gprs_ns_rx_unitdata(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800412 break;
413 case NS_PDUT_STATUS:
Harald Welte332347e2010-04-30 16:43:39 +0200414 rc = gprs_ns_rx_status(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800415 break;
416 case NS_PDUT_RESET:
Harald Welte332347e2010-04-30 16:43:39 +0200417 rc = gprs_ns_rx_reset(msg);
Harald Welte75bb8202010-03-14 15:45:01 +0800418 break;
419 case NS_PDUT_RESET_ACK:
Harald Weltef67a5f92010-04-26 19:18:54 +0200420 DEBUGP(DGPRS, "NS RESET ACK\n");
421 /* mark remote NS-VC as blocked + active */
422 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte75bb8202010-03-14 15:45:01 +0800423 break;
424 case NS_PDUT_UNBLOCK:
425 /* Section 7.2: unblocking procedure */
426 DEBUGP(DGPRS, "NS UNBLOCK\n");
427 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte7491c772010-03-18 00:01:43 +0800428 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte75bb8202010-03-14 15:45:01 +0800429 break;
430 case NS_PDUT_UNBLOCK_ACK:
Harald Weltef67a5f92010-04-26 19:18:54 +0200431 DEBUGP(DGPRS, "NS UNBLOCK ACK\n");
432 /* mark remote NS-VC as unblocked + active */
433 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte75bb8202010-03-14 15:45:01 +0800434 break;
435 case NS_PDUT_BLOCK:
436 DEBUGP(DGPRS, "NS BLOCK\n");
437 nsvc->state |= NSE_S_BLOCKED;
Harald Welte7491c772010-03-18 00:01:43 +0800438 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte75bb8202010-03-14 15:45:01 +0800439 break;
440 case NS_PDUT_BLOCK_ACK:
Harald Weltef67a5f92010-04-26 19:18:54 +0200441 DEBUGP(DGPRS, "NS BLOCK ACK\n");
442 /* mark remote NS-VC as blocked + active */
443 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte75bb8202010-03-14 15:45:01 +0800444 break;
445 default:
446 DEBUGP(DGPRS, "Unknown NS PDU type 0x%02x\n", nsh->pdu_type);
447 rc = -EINVAL;
448 break;
449 }
450 return rc;
451}
452
Harald Weltef67a5f92010-04-26 19:18:54 +0200453struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
454{
455 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
456
457 nsi->cb = cb;
458 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
459
460 return NULL;
461}
462
463void gprs_ns_destroy(struct gprs_ns_inst *nsi)
464{
465 /* FIXME: clear all timers */
466
467 /* recursively free the NSI and all its NSVCs */
468 talloc_free(nsi);
469}
470
471
472/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
473 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
474
475/* Read a single NS-over-IP message */
476static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
477 struct sockaddr_in *saddr)
478{
479 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
480 int ret = 0;
481 socklen_t saddr_len = sizeof(*saddr);
482
483 if (!msg) {
484 *error = -ENOMEM;
485 return NULL;
486 }
487
488 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
489 (struct sockaddr *)saddr, &saddr_len);
490 if (ret < 0) {
491 fprintf(stderr, "recv error %s\n", strerror(errno));
492 msgb_free(msg);
493 *error = ret;
494 return NULL;
495 } else if (ret == 0) {
496 msgb_free(msg);
497 *error = ret;
498 return NULL;
499 }
500
501 msg->l2h = msg->data;
502 msgb_put(msg, ret);
503
504 return msg;
505}
506
507static int handle_nsip_read(struct bsc_fd *bfd)
508{
509 int error;
510 struct sockaddr_in saddr;
511 struct gprs_ns_inst *nsi = bfd->data;
512 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
513
514 if (!msg)
515 return error;
516
517 return gprs_ns_rcvmsg(nsi, msg, &saddr);
518}
519
520static int handle_nsip_write(struct bsc_fd *bfd)
521{
522 /* FIXME: actually send the data here instead of nsip_sendmsg() */
523 return -EIO;
524}
525
526int nsip_sendmsg(struct msgb *msg, struct gprs_nsvc *nsvc)
527{
528 int rc;
529 struct gprs_ns_inst *nsi = nsvc->nsi;
530 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
531
532 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
533 (struct sockaddr *)daddr, sizeof(*daddr));
534
535 talloc_free(msg);
536
537 return rc;
538}
539
540/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
541static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
542{
543 int rc = 0;
544
545 if (what & BSC_FD_READ)
546 rc = handle_nsip_read(bfd);
547 if (what & BSC_FD_WRITE)
548 rc = handle_nsip_write(bfd);
549
550 return rc;
551}
552
553
554/* FIXME: this is currently in input/ipaccess.c */
555extern int make_sock(struct bsc_fd *bfd, int proto, u_int16_t port,
556 int (*cb)(struct bsc_fd *fd, unsigned int what));
557
558/* Listen for incoming GPRS packets */
559int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
560{
561 int ret;
562
563 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
564 if (ret < 0)
565 return ret;
566
567 nsi->ll = GPRS_NS_LL_UDP;
568 nsi->nsip.fd.data = nsi;
569
570 return ret;
571}