blob: 18d189f5a62e2343995d8880027c7b5b6dbf1291 [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 +020075/* Lookup struct gprs_nsvc based on NSVCI */
76static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi,
77 u_int16_t nsvci)
78{
79 struct gprs_nsvc *nsvc;
80 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
81 if (nsvc->nsvci == nsvci)
82 return nsvc;
83 }
84 return NULL;
85}
86
Harald Welte24a655f2010-04-30 19:54:29 +020087/* Lookup struct gprs_nsvc based on NSVCI */
88static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi,
89 u_int16_t nsei)
90{
91 struct gprs_nsvc *nsvc;
92 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
93 if (nsvc->nsei == nsei)
94 return nsvc;
95 }
96 return NULL;
97}
98
99
Harald Weltef030b212010-04-26 19:18:54 +0200100/* Lookup struct gprs_nsvc based on remote peer socket addr */
101static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
102 struct sockaddr_in *sin)
103{
104 struct gprs_nsvc *nsvc;
105 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
106 if (!memcmp(&nsvc->ip.bts_addr, sin, sizeof(*sin)))
107 return nsvc;
108 }
109 return NULL;
110}
111
112static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, u_int16_t nsvci)
113{
114 struct gprs_nsvc *nsvc;
115
116 nsvc = talloc_zero(nsi, struct gprs_nsvc);
117 nsvc->nsvci = nsvci;
118 /* before RESET procedure: BLOCKED and DEAD */
119 nsvc->state = NSE_S_BLOCKED;
120 nsvc->nsi = nsi;
121 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
122
123 return nsvc;
124}
Harald Welte9ba50052010-03-14 15:45:01 +0800125
126/* Section 10.3.2, Table 13 */
127static const char *ns_cause_str[] = {
128 [NS_CAUSE_TRANSIT_FAIL] = "Transit network failure",
129 [NS_CAUSE_OM_INTERVENTION] = "O&M intervention",
130 [NS_CAUSE_EQUIP_FAIL] = "Equipment failure",
131 [NS_CAUSE_NSVC_BLOCKED] = "NS-VC blocked",
132 [NS_CAUSE_NSVC_UNKNOWN] = "NS-VC unknown",
133 [NS_CAUSE_BVCI_UNKNOWN] = "BVCI unknown",
134 [NS_CAUSE_SEM_INCORR_PDU] = "Semantically incorrect PDU",
135 [NS_CAUSE_PDU_INCOMP_PSTATE] = "PDU not compatible with protocol state",
136 [NS_CAUSE_PROTO_ERR_UNSPEC] = "Protocol error, unspecified",
137 [NS_CAUSE_INVAL_ESSENT_IE] = "Invalid essential IE",
138 [NS_CAUSE_MISSING_ESSENT_IE] = "Missing essential IE",
139};
140
141static const char *gprs_ns_cause_str(enum ns_cause cause)
142{
143 if (cause >= ARRAY_SIZE(ns_cause_str))
144 return "undefined";
145
146 if (ns_cause_str[cause])
147 return ns_cause_str[cause];
148
149 return "undefined";
150}
151
Harald Welte24a655f2010-04-30 19:54:29 +0200152static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200153
Harald Welte24a655f2010-04-30 19:54:29 +0200154static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800155{
Harald Weltef030b212010-04-26 19:18:54 +0200156 int ret;
157
158 switch (nsvc->nsi->ll) {
159 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200160 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200161 break;
162 default:
163 LOGP(DGPRS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
164 msgb_free(msg);
165 ret = -EIO;
166 break;
167 }
168 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800169}
170
Harald Weltec5478482010-03-18 00:01:43 +0800171static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, u_int8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800172{
173 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
174 struct gprs_ns_hdr *nsh;
175
176 if (!msg)
177 return -ENOMEM;
178
179 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
180
181 nsh->pdu_type = pdu_type;
182
Harald Welte24a655f2010-04-30 19:54:29 +0200183 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800184}
185
186#define NS_TIMER_ALIVE 3, 0 /* after 3 seconds without response, we retry */
187#define NS_TIMER_TEST 30, 0 /* every 10 seconds we check if the BTS is still alive */
188#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
189
190static void gprs_ns_alive_cb(void *data)
191{
192 struct gprs_nsvc *nsvc = data;
193
194 if (nsvc->timer_is_tns_alive) {
195 /* Tns-alive case: we expired without response ! */
196 nsvc->alive_retries++;
197 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
198 /* mark as dead and blocked */
199 nsvc->state = NSE_S_BLOCKED;
200 DEBUGP(DGPRS, "Tns-alive more then %u retries, "
201 " blocking NS-VC\n", NS_ALIVE_RETRIES);
202 /* FIXME: inform higher layers */
203 return;
204 }
205 } else {
206 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltec5478482010-03-18 00:01:43 +0800207 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800208 /* start Tns-alive timer */
209 nsvc->timer_is_tns_alive = 1;
210 }
211 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
212}
213
214/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800215static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800216{
217 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
218 struct gprs_ns_hdr *nsh;
Harald Weltec5478482010-03-18 00:01:43 +0800219 u_int16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800220
221 if (!msg)
222 return -ENOMEM;
223
Harald Weltec5478482010-03-18 00:01:43 +0800224 nsvci = htons(nsvc->nsvci);
225 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800226
227 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
228
229 nsh->pdu_type = NS_PDUT_RESET_ACK;
230
Harald Weltec5478482010-03-18 00:01:43 +0800231 DEBUGP(DGPRS, "nsvci=%u, nsei=%u\n", nsvc->nsvci, nsvc->nsei);
232
Harald Welte9ba50052010-03-14 15:45:01 +0800233 msgb_tvlv_put(msg, NS_IE_VCI, 2, (u_int8_t *)&nsvci);
234 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (u_int8_t *)&nsei);
235
Harald Welte24a655f2010-04-30 19:54:29 +0200236 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800237}
238
Harald Welte24a655f2010-04-30 19:54:29 +0200239/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
240int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800241{
Harald Welte24a655f2010-04-30 19:54:29 +0200242 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800243 struct gprs_ns_hdr *nsh;
Harald Welte24a655f2010-04-30 19:54:29 +0200244 u_int16_t bvci = msgb_bvci(msg);
245
246 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
247 if (!nsvc) {
248 DEBUGP(DGPRS, "Unable to resolve NSEI %u to NS-VC!\n", msgb_nsei(msg));
249 return -EINVAL;
250 }
Harald Welte9ba50052010-03-14 15:45:01 +0800251
252 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
253 if (!nsh) {
254 DEBUGP(DGPRS, "Not enough headroom for NS header\n");
255 return -EIO;
256 }
257
258 nsh->pdu_type = NS_PDUT_UNITDATA;
259 /* spare octet in data[0] */
260 nsh->data[1] = bvci >> 8;
261 nsh->data[2] = bvci & 0xff;
262
Harald Welte24a655f2010-04-30 19:54:29 +0200263 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800264}
265
266/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200267static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800268{
269 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
270 u_int16_t bvci;
271
272 /* spare octet in data[0] */
273 bvci = nsh->data[1] << 8 | nsh->data[2];
274 msg->l3h = &nsh->data[3];
275
276 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200277 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800278}
279
280/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200281static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800282{
Harald Weltef030b212010-04-26 19:18:54 +0200283 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800284 struct tlv_parsed tp;
285 u_int8_t cause;
286 int rc;
287
288 DEBUGP(DGPRS, "NS STATUS ");
289
290 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
291
292 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
293 DEBUGPC(DGPRS, "missing cause IE\n");
294 return -EINVAL;
295 }
296
297 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
298 DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause));
299
300 return 0;
301}
302
303/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200304static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800305{
306 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800307 struct tlv_parsed tp;
308 u_int8_t *cause;
309 u_int16_t *nsvci, *nsei;
310 int rc;
311
312 DEBUGP(DGPRS, "NS RESET ");
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 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
318 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
319 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
320 DEBUGPC(DGPRS, "Missing mandatory IE\n");
321 return -EINVAL;
322 }
323
324 cause = (u_int8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
325 nsvci = (u_int16_t *) TLVP_VAL(&tp, NS_IE_VCI);
326 nsei = (u_int16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
327
Harald Weltec5478482010-03-18 00:01:43 +0800328 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
329 nsvc->nsei = ntohs(*nsei);
330 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800331
332 DEBUGPC(DGPRS, "cause=%s, NSVCI=%u, NSEI=%u\n",
Harald Weltec5478482010-03-18 00:01:43 +0800333 gprs_ns_cause_str(*cause), nsvc->nsvci, nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800334
335 /* mark the NS-VC as blocked and alive */
Harald Welte9ba50052010-03-14 15:45:01 +0800336 /* start the test procedure */
337 nsvc->alive_timer.cb = gprs_ns_alive_cb;
338 nsvc->alive_timer.data = nsvc;
339 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE);
340
Harald Weltec5478482010-03-18 00:01:43 +0800341 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800342}
343
344/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200345int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
346 struct sockaddr_in *saddr)
Harald Welte9ba50052010-03-14 15:45:01 +0800347{
348 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200349 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800350 int rc = 0;
351
Harald Weltef030b212010-04-26 19:18:54 +0200352 /* look up the NSVC based on source address */
353 nsvc = nsvc_by_rem_addr(nsi, saddr);
354 if (!nsvc) {
355 /* Only the RESET procedure creates a new NSVC */
356 if (nsh->pdu_type != NS_PDUT_RESET)
357 return -EIO;
358 nsvc = nsvc_create(nsi, 0xffff);
359 nsvc->ip.bts_addr = *saddr;
Harald Welte24a655f2010-04-30 19:54:29 +0200360 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200361 return rc;
362 }
Harald Welte24a655f2010-04-30 19:54:29 +0200363 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800364
Harald Welte9ba50052010-03-14 15:45:01 +0800365 switch (nsh->pdu_type) {
366 case NS_PDUT_ALIVE:
367 /* remote end inquires whether we're still alive,
368 * we need to respond with ALIVE_ACK */
Harald Weltec5478482010-03-18 00:01:43 +0800369 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800370 break;
371 case NS_PDUT_ALIVE_ACK:
372 /* stop Tns-alive */
373 bsc_del_timer(&nsvc->alive_timer);
374 /* start Tns-test */
375 nsvc->timer_is_tns_alive = 0;
376 bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_TEST);
377 break;
378 case NS_PDUT_UNITDATA:
379 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200380 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800381 break;
382 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200383 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800384 break;
385 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200386 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800387 break;
388 case NS_PDUT_RESET_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200389 DEBUGP(DGPRS, "NS RESET ACK\n");
390 /* mark remote NS-VC as blocked + active */
391 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800392 break;
393 case NS_PDUT_UNBLOCK:
394 /* Section 7.2: unblocking procedure */
395 DEBUGP(DGPRS, "NS UNBLOCK\n");
396 nsvc->state &= ~NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800397 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800398 break;
399 case NS_PDUT_UNBLOCK_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200400 DEBUGP(DGPRS, "NS UNBLOCK ACK\n");
401 /* mark remote NS-VC as unblocked + active */
402 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800403 break;
404 case NS_PDUT_BLOCK:
405 DEBUGP(DGPRS, "NS BLOCK\n");
406 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800407 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800408 break;
409 case NS_PDUT_BLOCK_ACK:
Harald Weltef030b212010-04-26 19:18:54 +0200410 DEBUGP(DGPRS, "NS BLOCK ACK\n");
411 /* mark remote NS-VC as blocked + active */
412 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800413 break;
414 default:
415 DEBUGP(DGPRS, "Unknown NS PDU type 0x%02x\n", nsh->pdu_type);
416 rc = -EINVAL;
417 break;
418 }
419 return rc;
420}
421
Harald Weltef030b212010-04-26 19:18:54 +0200422struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
423{
424 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
425
426 nsi->cb = cb;
427 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
428
Harald Welte3771d092010-04-30 20:26:32 +0200429 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200430}
431
432void gprs_ns_destroy(struct gprs_ns_inst *nsi)
433{
434 /* FIXME: clear all timers */
435
436 /* recursively free the NSI and all its NSVCs */
437 talloc_free(nsi);
438}
439
440
441/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
442 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
443
444/* Read a single NS-over-IP message */
445static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
446 struct sockaddr_in *saddr)
447{
448 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
449 int ret = 0;
450 socklen_t saddr_len = sizeof(*saddr);
451
452 if (!msg) {
453 *error = -ENOMEM;
454 return NULL;
455 }
456
457 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
458 (struct sockaddr *)saddr, &saddr_len);
459 if (ret < 0) {
460 fprintf(stderr, "recv error %s\n", strerror(errno));
461 msgb_free(msg);
462 *error = ret;
463 return NULL;
464 } else if (ret == 0) {
465 msgb_free(msg);
466 *error = ret;
467 return NULL;
468 }
469
470 msg->l2h = msg->data;
471 msgb_put(msg, ret);
472
473 return msg;
474}
475
476static int handle_nsip_read(struct bsc_fd *bfd)
477{
478 int error;
479 struct sockaddr_in saddr;
480 struct gprs_ns_inst *nsi = bfd->data;
481 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
482
483 if (!msg)
484 return error;
485
486 return gprs_ns_rcvmsg(nsi, msg, &saddr);
487}
488
489static int handle_nsip_write(struct bsc_fd *bfd)
490{
491 /* FIXME: actually send the data here instead of nsip_sendmsg() */
492 return -EIO;
493}
494
Harald Welte24a655f2010-04-30 19:54:29 +0200495int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200496{
497 int rc;
498 struct gprs_ns_inst *nsi = nsvc->nsi;
499 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
500
501 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
502 (struct sockaddr *)daddr, sizeof(*daddr));
503
504 talloc_free(msg);
505
506 return rc;
507}
508
509/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
510static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
511{
512 int rc = 0;
513
514 if (what & BSC_FD_READ)
515 rc = handle_nsip_read(bfd);
516 if (what & BSC_FD_WRITE)
517 rc = handle_nsip_write(bfd);
518
519 return rc;
520}
521
522
523/* FIXME: this is currently in input/ipaccess.c */
524extern int make_sock(struct bsc_fd *bfd, int proto, u_int16_t port,
525 int (*cb)(struct bsc_fd *fd, unsigned int what));
526
527/* Listen for incoming GPRS packets */
528int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
529{
530 int ret;
531
532 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
533 if (ret < 0)
534 return ret;
535
536 nsi->ll = GPRS_NS_LL_UDP;
537 nsi->nsip.fd.data = nsi;
538
539 return ret;
540}
Harald Welte3771d092010-04-30 20:26:32 +0200541
542/* Establish a connection (from the BSS) to the SGSN */
543struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200544 struct sockaddr_in *dest, uint16_t nsei,
545 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200546{
547 struct gprs_nsvc *nsvc;
548
549 nsvc = nsvc_by_rem_addr(nsi, dest);
550 if (!nsvc) {
551 nsvc = nsvc_create(nsi, nsvci);
552 nsvc->ip.bts_addr = *dest;
553 }
Harald Welte1203de32010-05-01 11:28:43 +0200554 nsvc->nsei = nsei;
555 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200556 nsvc->remote_end_is_sgsn = 1;
557
558 /* Initiate a RESET procedure */
559 if (gprs_ns_tx_simple(nsvc, NS_PDUT_RESET) < 0)
560 return NULL;
561 /* FIXME: should we run a timer and re-transmit the reset request? */
562
563 return nsvc;
564}