blob: c5166fa535121ccdf1e63280be87bbf781897d2a [file] [log] [blame]
Harald Welte9ba50052010-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 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
75#define NSE_S_BLOCKED 0x0001
76#define NSE_S_ALIVE 0x0002
77
78struct gprs_nsvc {
79 struct llist_head list;
Harald Weltef030b212010-04-26 19:18:54 +020080 struct gprs_ns_inst *nsi;
Harald Welte9ba50052010-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 Weltef030b212010-04-26 19:18:54 +020086 u_int32_t remote_state;
Harald Welte9ba50052010-03-14 15:45:01 +080087
88 struct timer_list alive_timer;
89 int timer_is_tns_alive;
90 int alive_retries;
Harald Weltec5478482010-03-18 00:01:43 +080091
92 union {
93 struct {
94 struct sockaddr_in bts_addr;
95 } ip;
96 };
Harald Welte9ba50052010-03-14 15:45:01 +080097};
98
Harald Weltef030b212010-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 Welte9ba50052010-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 Weltef030b212010-04-26 19:18:54 +0200187static int nsip_sendmsg(struct msgb *msg, struct gprs_nsvc *nsvc);
188
Harald Weltec5478482010-03-18 00:01:43 +0800189static int gprs_ns_tx(struct msgb *msg, struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800190{
Harald Weltef030b212010-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 Welte9ba50052010-03-14 15:45:01 +0800204}
205
Harald Weltec5478482010-03-18 00:01:43 +0800206static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, u_int8_t pdu_type)
Harald Welte9ba50052010-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 Weltec5478482010-03-18 00:01:43 +0800218 return gprs_ns_tx(msg, nsvc);
Harald Welte9ba50052010-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 Weltec5478482010-03-18 00:01:43 +0800242 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Welte9ba50052010-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 Weltec5478482010-03-18 00:01:43 +0800250static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800251{
252 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
253 struct gprs_ns_hdr *nsh;
Harald Weltec5478482010-03-18 00:01:43 +0800254 u_int16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800255
256 if (!msg)
257 return -ENOMEM;
258
Harald Weltec5478482010-03-18 00:01:43 +0800259 nsvci = htons(nsvc->nsvci);
260 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-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 Weltec5478482010-03-18 00:01:43 +0800266 DEBUGP(DGPRS, "nsvci=%u, nsei=%u\n", nsvc->nsvci, nsvc->nsei);
267
Harald Welte9ba50052010-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 Weltec5478482010-03-18 00:01:43 +0800271 return gprs_ns_tx(msg, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800272}
273
274/* Section 9.2.10: transmit side */
Harald Weltef030b212010-04-26 19:18:54 +0200275int gprs_ns_sendmsg(struct gprs_nsvc *nsvc, u_int16_t bvci,
Harald Welte9ba50052010-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 Weltec5478482010-03-18 00:01:43 +0800291 return gprs_ns_tx(msg, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800292}
293
294/* Section 9.2.10: receive side */
Harald Weltef030b212010-04-26 19:18:54 +0200295static int gprs_ns_rx_unitdata(struct msgb *msg, struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800296{
297 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
298 u_int16_t bvci;
299
300 /* spare octet in data[0] */
301 bvci = nsh->data[1] << 8 | nsh->data[2];
302 msg->l3h = &nsh->data[3];
303
304 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200305 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800306}
307
308/* Section 9.2.7 */
Harald Weltef030b212010-04-26 19:18:54 +0200309static int gprs_ns_rx_status(struct msgb *msg, struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800310{
Harald Weltef030b212010-04-26 19:18:54 +0200311 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800312 struct tlv_parsed tp;
313 u_int8_t cause;
314 int rc;
315
316 DEBUGP(DGPRS, "NS STATUS ");
317
318 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
319
320 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
321 DEBUGPC(DGPRS, "missing cause IE\n");
322 return -EINVAL;
323 }
324
325 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
326 DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause));
327
328 return 0;
329}
330
331/* Section 7.3 */
Harald Weltef030b212010-04-26 19:18:54 +0200332static int gprs_ns_rx_reset(struct msgb *msg, struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800333{
334 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800335 struct tlv_parsed tp;
336 u_int8_t *cause;
337 u_int16_t *nsvci, *nsei;
338 int rc;
339
340 DEBUGP(DGPRS, "NS RESET ");
341
342 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
343
344 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
345 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
346 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
347 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
348 DEBUGPC(DGPRS, "Missing mandatory IE\n");
349 return -EINVAL;
350 }
351
352 cause = (u_int8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
353 nsvci = (u_int16_t *) TLVP_VAL(&tp, NS_IE_VCI);
354 nsei = (u_int16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
355
Harald Weltec5478482010-03-18 00:01:43 +0800356 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
357 nsvc->nsei = ntohs(*nsei);
358 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800359
360 DEBUGPC(DGPRS, "cause=%s, NSVCI=%u, NSEI=%u\n",
Harald Weltec5478482010-03-18 00:01:43 +0800361 gprs_ns_cause_str(*cause), nsvc->nsvci, nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800362
363 /* mark the NS-VC as blocked and alive */
Harald Welte9ba50052010-03-14 15:45:01 +0800364 /* start the test procedure */
365 nsvc->alive_timer.cb = gprs_ns_alive_cb;
366 nsvc->alive_timer.data = nsvc;
367 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
368
Harald Weltec5478482010-03-18 00:01:43 +0800369 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800370}
371
372/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200373int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
374 struct sockaddr_in *saddr)
Harald Welte9ba50052010-03-14 15:45:01 +0800375{
376 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200377 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800378 int rc = 0;
379
Harald Weltef030b212010-04-26 19:18:54 +0200380 /* look up the NSVC based on source address */
381 nsvc = nsvc_by_rem_addr(nsi, saddr);
382 if (!nsvc) {
383 /* Only the RESET procedure creates a new NSVC */
384 if (nsh->pdu_type != NS_PDUT_RESET)
385 return -EIO;
386 nsvc = nsvc_create(nsi, 0xffff);
387 nsvc->ip.bts_addr = *saddr;
388 rc = gprs_ns_rx_reset(msg, nsvc);
389 return rc;
390 }
Harald Weltec5478482010-03-18 00:01:43 +0800391
Harald Welte9ba50052010-03-14 15:45:01 +0800392 switch (nsh->pdu_type) {
393 case NS_PDUT_ALIVE:
394 /* remote end inquires whether we're still alive,
395 * we need to respond with ALIVE_ACK */
Harald Weltec5478482010-03-18 00:01:43 +0800396 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800397 break;
398 case NS_PDUT_ALIVE_ACK:
399 /* stop Tns-alive */
400 bsc_del_timer(&nsvc->alive_timer);
401 /* start Tns-test */
402 nsvc->timer_is_tns_alive = 0;
403 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_TEST);
404 break;
405 case NS_PDUT_UNITDATA:
406 /* actual user data */
Harald Weltef030b212010-04-26 19:18:54 +0200407 rc = gprs_ns_rx_unitdata(msg, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800408 break;
409 case NS_PDUT_STATUS:
Harald Weltef030b212010-04-26 19:18:54 +0200410 rc = gprs_ns_rx_status(msg, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800411 break;
412 case NS_PDUT_RESET:
Harald Weltef030b212010-04-26 19:18:54 +0200413 rc = gprs_ns_rx_reset(msg, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800414 break;
415 case NS_PDUT_RESET_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200416 DEBUGP(DGPRS, "NS RESET ACK\n");
417 /* mark remote NS-VC as blocked + active */
418 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800419 break;
420 case NS_PDUT_UNBLOCK:
421 /* Section 7.2: unblocking procedure */
422 DEBUGP(DGPRS, "NS UNBLOCK\n");
423 nsvc->state &= ~NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800424 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800425 break;
426 case NS_PDUT_UNBLOCK_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200427 DEBUGP(DGPRS, "NS UNBLOCK ACK\n");
428 /* mark remote NS-VC as unblocked + active */
429 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800430 break;
431 case NS_PDUT_BLOCK:
432 DEBUGP(DGPRS, "NS BLOCK\n");
433 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800434 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800435 break;
436 case NS_PDUT_BLOCK_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200437 DEBUGP(DGPRS, "NS BLOCK ACK\n");
438 /* mark remote NS-VC as blocked + active */
439 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800440 break;
441 default:
442 DEBUGP(DGPRS, "Unknown NS PDU type 0x%02x\n", nsh->pdu_type);
443 rc = -EINVAL;
444 break;
445 }
446 return rc;
447}
448
Harald Weltef030b212010-04-26 19:18:54 +0200449struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
450{
451 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
452
453 nsi->cb = cb;
454 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
455
456 return NULL;
457}
458
459void gprs_ns_destroy(struct gprs_ns_inst *nsi)
460{
461 /* FIXME: clear all timers */
462
463 /* recursively free the NSI and all its NSVCs */
464 talloc_free(nsi);
465}
466
467
468/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
469 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
470
471/* Read a single NS-over-IP message */
472static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
473 struct sockaddr_in *saddr)
474{
475 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
476 int ret = 0;
477 socklen_t saddr_len = sizeof(*saddr);
478
479 if (!msg) {
480 *error = -ENOMEM;
481 return NULL;
482 }
483
484 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
485 (struct sockaddr *)saddr, &saddr_len);
486 if (ret < 0) {
487 fprintf(stderr, "recv error %s\n", strerror(errno));
488 msgb_free(msg);
489 *error = ret;
490 return NULL;
491 } else if (ret == 0) {
492 msgb_free(msg);
493 *error = ret;
494 return NULL;
495 }
496
497 msg->l2h = msg->data;
498 msgb_put(msg, ret);
499
500 return msg;
501}
502
503static int handle_nsip_read(struct bsc_fd *bfd)
504{
505 int error;
506 struct sockaddr_in saddr;
507 struct gprs_ns_inst *nsi = bfd->data;
508 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
509
510 if (!msg)
511 return error;
512
513 return gprs_ns_rcvmsg(nsi, msg, &saddr);
514}
515
516static int handle_nsip_write(struct bsc_fd *bfd)
517{
518 /* FIXME: actually send the data here instead of nsip_sendmsg() */
519 return -EIO;
520}
521
522int nsip_sendmsg(struct msgb *msg, struct gprs_nsvc *nsvc)
523{
524 int rc;
525 struct gprs_ns_inst *nsi = nsvc->nsi;
526 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
527
528 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
529 (struct sockaddr *)daddr, sizeof(*daddr));
530
531 talloc_free(msg);
532
533 return rc;
534}
535
536/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
537static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
538{
539 int rc = 0;
540
541 if (what & BSC_FD_READ)
542 rc = handle_nsip_read(bfd);
543 if (what & BSC_FD_WRITE)
544 rc = handle_nsip_write(bfd);
545
546 return rc;
547}
548
549
550/* FIXME: this is currently in input/ipaccess.c */
551extern int make_sock(struct bsc_fd *bfd, int proto, u_int16_t port,
552 int (*cb)(struct bsc_fd *fd, unsigned int what));
553
554/* Listen for incoming GPRS packets */
555int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
556{
557 int ret;
558
559 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
560 if (ret < 0)
561 return ret;
562
563 nsi->ll = GPRS_NS_LL_UDP;
564 nsi->nsip.fd.data = nsi;
565
566 return ret;
567}