blob: 3fa2c01693c99980c72e35659093b5dc53748934 [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>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020050#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080051
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>
Harald Welte834f26d2010-05-11 06:20:54 +020060#include <openbsc/signal.h>
Harald Welte9ba50052010-03-14 15:45:01 +080061#include <openbsc/gprs_ns.h>
62#include <openbsc/gprs_bssgp.h>
63
64#define NS_ALLOC_SIZE 1024
65
66static const struct tlv_definition ns_att_tlvdef = {
67 .def = {
68 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
69 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
70 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
71 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
72 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
73 },
74};
75
Harald Weltef030b212010-04-26 19:18:54 +020076/* Lookup struct gprs_nsvc based on NSVCI */
77static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020078 uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +020079{
80 struct gprs_nsvc *nsvc;
81 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
82 if (nsvc->nsvci == nsvci)
83 return nsvc;
84 }
85 return NULL;
86}
87
Harald Welte24a655f2010-04-30 19:54:29 +020088/* Lookup struct gprs_nsvc based on NSVCI */
89static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020090 uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +020091{
92 struct gprs_nsvc *nsvc;
93 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
94 if (nsvc->nsei == nsei)
95 return nsvc;
96 }
97 return NULL;
98}
99
100
Harald Weltef030b212010-04-26 19:18:54 +0200101/* Lookup struct gprs_nsvc based on remote peer socket addr */
102static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
103 struct sockaddr_in *sin)
104{
105 struct gprs_nsvc *nsvc;
106 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
107 if (!memcmp(&nsvc->ip.bts_addr, sin, sizeof(*sin)))
108 return nsvc;
109 }
110 return NULL;
111}
112
Harald Welte69a4cf22010-05-03 20:55:10 +0200113static void gprs_ns_timer_cb(void *data);
114
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200115static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200116{
117 struct gprs_nsvc *nsvc;
118
119 nsvc = talloc_zero(nsi, struct gprs_nsvc);
120 nsvc->nsvci = nsvci;
121 /* before RESET procedure: BLOCKED and DEAD */
122 nsvc->state = NSE_S_BLOCKED;
123 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200124 nsvc->timer.cb = gprs_ns_timer_cb;
125 nsvc->timer.data = nsvc;
126
Harald Weltef030b212010-04-26 19:18:54 +0200127 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
128
129 return nsvc;
130}
Harald Welte9ba50052010-03-14 15:45:01 +0800131
Harald Welte834f26d2010-05-11 06:20:54 +0200132static void ns_dispatch_signal(struct nsvc *nsvc, unsigned int signal,
133 uint8_t cause)
134{
135 struct ns_signal_data nssd;
136
137 nssd.nsvc = nsvc;
138 nssd.cause = cause;
139
140 dispatch_signal(SS_NS, signal, &nssd);
141}
142
Harald Welte9ba50052010-03-14 15:45:01 +0800143/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200144static const struct value_string ns_cause_str[] = {
145 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
146 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
147 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
148 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
149 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
150 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
151 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
152 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200153 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200154 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
155 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
156 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800157};
158
Harald Welte2577d412010-05-02 09:37:45 +0200159const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800160{
Harald Welte2577d412010-05-02 09:37:45 +0200161 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800162}
163
Harald Welte24a655f2010-04-30 19:54:29 +0200164static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200165
Harald Welte24a655f2010-04-30 19:54:29 +0200166static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800167{
Harald Weltef030b212010-04-26 19:18:54 +0200168 int ret;
169
170 switch (nsvc->nsi->ll) {
171 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200172 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200173 break;
174 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200175 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200176 msgb_free(msg);
177 ret = -EIO;
178 break;
179 }
180 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800181}
182
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200183static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800184{
185 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
186 struct gprs_ns_hdr *nsh;
187
188 if (!msg)
189 return -ENOMEM;
190
191 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
192
193 nsh->pdu_type = pdu_type;
194
Harald Welte24a655f2010-04-30 19:54:29 +0200195 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800196}
197
Harald Welte834f26d2010-05-11 06:20:54 +0200198int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200199{
200 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
201 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200202 uint16_t nsvci = htons(nsvc->nsvci);
203 uint16_t nsei = htons(nsvc->nsei);
204
205 if (!msg)
206 return -ENOMEM;
207
208 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
209 nsh->pdu_type = NS_PDUT_RESET;
210
211 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
212 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
213 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
214
215 return gprs_ns_tx(nsvc, msg);
216
217}
218
Harald Welte834f26d2010-05-11 06:20:54 +0200219int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
220{
221 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
222 struct gprs_ns_hdr *nsh;
223 uint16_t nsvci = htons(nsvc->nsvci);
224
225 if (!msg)
226 return -ENOMEM;
227
228 /* be conservative and mark it as blocked even now! */
229 nsvc->state |= NSE_S_BLOCKED;
230
231 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
232 nsh->pdu_type = NS_PDUT_BLOCK;
233
234 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
235 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
236
237 return gprs_ns_tx(nsvc, msg);
238}
239
240int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
241{
242 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
243}
244
Harald Welte9ba50052010-03-14 15:45:01 +0800245#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
246
Harald Welte69a4cf22010-05-03 20:55:10 +0200247static const uint8_t timer_mode_tout[_NSVC_TIMER_NR] = {
248 [NSVC_TIMER_TNS_RESET] = 60,
249 [NSVC_TIMER_TNS_ALIVE] = 3,
250 [NSVC_TIMER_TNS_TEST] = 30,
251};
252
253static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
254{
255 nsvc->alive_retries = 0;
256
257 if (bsc_timer_pending(&nsvc->timer))
258 bsc_del_timer(&nsvc->timer);
259
260 nsvc->timer_mode = mode;
261 bsc_schedule_timer(&nsvc->timer, timer_mode_tout[mode], 0);
262}
263
Harald Welte80405452010-05-03 20:16:13 +0200264static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800265{
266 struct gprs_nsvc *nsvc = data;
267
Harald Welte80405452010-05-03 20:16:13 +0200268 switch (nsvc->timer_mode) {
269 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800270 /* Tns-alive case: we expired without response ! */
271 nsvc->alive_retries++;
272 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
273 /* mark as dead and blocked */
274 nsvc->state = NSE_S_BLOCKED;
Harald Welteb8a6a832010-05-11 05:54:22 +0200275 DEBUGP(DNS, "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200276 "%u times, blocking NS-VC\n", nsvc->nsei,
277 NS_ALIVE_RETRIES);
Harald Welte9ba50052010-03-14 15:45:01 +0800278 /* FIXME: inform higher layers */
279 return;
280 }
Harald Welte69a4cf22010-05-03 20:55:10 +0200281 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200282 break;
283 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800284 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltec5478482010-03-18 00:01:43 +0800285 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800286 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200287 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
288 break;
289 case NSVC_TIMER_TNS_RESET:
290 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200291 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Welte69a4cf22010-05-03 20:55:10 +0200292 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200293 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800294 }
Harald Welte9ba50052010-03-14 15:45:01 +0800295}
296
297/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800298static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800299{
300 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
301 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200302 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800303
304 if (!msg)
305 return -ENOMEM;
306
Harald Weltec5478482010-03-18 00:01:43 +0800307 nsvci = htons(nsvc->nsvci);
308 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800309
310 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
311
312 nsh->pdu_type = NS_PDUT_RESET_ACK;
313
Harald Welteb8a6a832010-05-11 05:54:22 +0200314 DEBUGP(DNS, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200315 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800316
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200317 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
318 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800319
Harald Welte24a655f2010-04-30 19:54:29 +0200320 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800321}
322
Harald Welte24a655f2010-04-30 19:54:29 +0200323/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
324int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800325{
Harald Welte24a655f2010-04-30 19:54:29 +0200326 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800327 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200328 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200329
330 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
331 if (!nsvc) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200332 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200333 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200334 return -EINVAL;
335 }
Harald Welte9ba50052010-03-14 15:45:01 +0800336
Harald Welte69a4cf22010-05-03 20:55:10 +0200337 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200338 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200339 nsvc->nsei);
340 return -EBUSY;
341 }
342 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200343 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200344 nsvc->nsei);
345 return -EBUSY;
346 }
347
Harald Welte9ba50052010-03-14 15:45:01 +0800348 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
349 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200350 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800351 return -EIO;
352 }
353
354 nsh->pdu_type = NS_PDUT_UNITDATA;
355 /* spare octet in data[0] */
356 nsh->data[1] = bvci >> 8;
357 nsh->data[2] = bvci & 0xff;
358
Harald Welte24a655f2010-04-30 19:54:29 +0200359 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800360}
361
362/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200363static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800364{
365 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200366 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800367
368 /* spare octet in data[0] */
369 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200370 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200371 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800372
373 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200374 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800375}
376
377/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200378static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800379{
Harald Weltef030b212010-04-26 19:18:54 +0200380 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800381 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200382 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800383 int rc;
384
Harald Welteb8a6a832010-05-11 05:54:22 +0200385 DEBUGP(DNS, "NSEI=%u NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800386
387 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
388
389 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200390 DEBUGPC(DNS, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800391 return -EINVAL;
392 }
393
394 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welteb8a6a832010-05-11 05:54:22 +0200395 DEBUGPC(DNS, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800396
397 return 0;
398}
399
400/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200401static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800402{
403 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800404 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200405 uint8_t *cause;
406 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800407 int rc;
408
Harald Welte9ba50052010-03-14 15:45:01 +0800409 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
410
411 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
412 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
413 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
414 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
Harald Welteb8a6a832010-05-11 05:54:22 +0200415 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800416 return -EINVAL;
417 }
418
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200419 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
420 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
421 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800422
Harald Welteb8a6a832010-05-11 05:54:22 +0200423 DEBUGP(DNS, "NSEI=%u NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200424 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
425
Harald Weltec5478482010-03-18 00:01:43 +0800426 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
427 nsvc->nsei = ntohs(*nsei);
428 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800429
Harald Welte9ba50052010-03-14 15:45:01 +0800430 /* mark the NS-VC as blocked and alive */
Harald Welte9ba50052010-03-14 15:45:01 +0800431 /* start the test procedure */
Harald Welte69a4cf22010-05-03 20:55:10 +0200432 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800433
Harald Welte834f26d2010-05-11 06:20:54 +0200434 /* inform interested parties about the fact that this NSVC
435 * has received RESET */
436 ns_dispatch_signal(nsvc, S_NS_RESET, cause);
437
Harald Weltec5478482010-03-18 00:01:43 +0800438 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800439}
440
Harald Welte834f26d2010-05-11 06:20:54 +0200441static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
442{
443 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
444 struct tlv_parsed tp;
445 uint8_t *cause;
446 int rc;
447
448 DEBUGP(DNS, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
449
450 nsvc->state |= NSE_S_BLOCKED;
451
452 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
453
454 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
455 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
456 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
457 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
458 return -EINVAL;
459 }
460
461 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
462 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
463
464 ns_dispatch_signal(nsvc, S_NS_BLOCK, cause);
465
466 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
467}
468
Harald Welte9ba50052010-03-14 15:45:01 +0800469/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200470int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
471 struct sockaddr_in *saddr)
Harald Welte9ba50052010-03-14 15:45:01 +0800472{
473 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200474 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800475 int rc = 0;
476
Harald Weltef030b212010-04-26 19:18:54 +0200477 /* look up the NSVC based on source address */
478 nsvc = nsvc_by_rem_addr(nsi, saddr);
479 if (!nsvc) {
480 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200481 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200482 LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x "
Harald Weltebbc9fac2010-05-03 18:54:12 +0200483 "from %s for non-existing NS-VC\n",
484 nsh->pdu_type, inet_ntoa(saddr->sin_addr));
Harald Welte570fb832010-05-03 21:05:24 +0200485 //gprs_ns_tx_reset(nsvc, NS_CAUSE_NSVC_UNKNOWN);
Harald Weltef030b212010-04-26 19:18:54 +0200486 return -EIO;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200487 }
Harald Welteb8a6a832010-05-11 05:54:22 +0200488 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200489 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Weltef030b212010-04-26 19:18:54 +0200490 nsvc = nsvc_create(nsi, 0xffff);
491 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200492 } else
493 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800494
Harald Welte9ba50052010-03-14 15:45:01 +0800495 switch (nsh->pdu_type) {
496 case NS_PDUT_ALIVE:
497 /* remote end inquires whether we're still alive,
498 * we need to respond with ALIVE_ACK */
Harald Weltec5478482010-03-18 00:01:43 +0800499 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800500 break;
501 case NS_PDUT_ALIVE_ACK:
502 /* stop Tns-alive */
Harald Welte80405452010-05-03 20:16:13 +0200503 bsc_del_timer(&nsvc->timer);
Harald Welte9ba50052010-03-14 15:45:01 +0800504 /* start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200505 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200506 if (nsvc->remote_end_is_sgsn) {
507 /* FIXME: this should be one level higher */
508 if (nsvc->state & NSE_S_BLOCKED)
509 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
510 }
Harald Welte9ba50052010-03-14 15:45:01 +0800511 break;
512 case NS_PDUT_UNITDATA:
513 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200514 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800515 break;
516 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200517 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800518 break;
519 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200520 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800521 break;
522 case NS_PDUT_RESET_ACK:
Harald Welteb8a6a832010-05-11 05:54:22 +0200523 DEBUGP(DNS, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200524 /* mark remote NS-VC as blocked + active */
525 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte69a4cf22010-05-03 20:55:10 +0200526 if (nsvc->remote_end_is_sgsn) {
527 /* stop RESET timer */
528 bsc_del_timer(&nsvc->timer);
Harald Welte570fb832010-05-03 21:05:24 +0200529 /* send ALIVE PDU */
530 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
531 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte7fb7e612010-05-03 21:11:22 +0200532 /* mark local state as BLOCKED + ALIVE */
533 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte69a4cf22010-05-03 20:55:10 +0200534 }
Harald Welte9ba50052010-03-14 15:45:01 +0800535 break;
536 case NS_PDUT_UNBLOCK:
537 /* Section 7.2: unblocking procedure */
Harald Welteb8a6a832010-05-11 05:54:22 +0200538 DEBUGP(DNS, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800539 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte834f26d2010-05-11 06:20:54 +0200540 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +0800541 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800542 break;
543 case NS_PDUT_UNBLOCK_ACK:
Harald Welteb8a6a832010-05-11 05:54:22 +0200544 DEBUGP(DNS, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200545 /* mark remote NS-VC as unblocked + active */
546 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte7fb7e612010-05-03 21:11:22 +0200547 if (nsvc->remote_end_is_sgsn)
548 nsvc->state = NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800549 break;
550 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +0200551 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800552 break;
553 case NS_PDUT_BLOCK_ACK:
Harald Welteb8a6a832010-05-11 05:54:22 +0200554 DEBUGP(DNS, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200555 /* mark remote NS-VC as blocked + active */
556 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800557 break;
558 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200559 DEBUGP(DNS, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200560 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800561 rc = -EINVAL;
562 break;
563 }
564 return rc;
565}
566
Harald Weltef030b212010-04-26 19:18:54 +0200567struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
568{
569 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
570
571 nsi->cb = cb;
572 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
573
Harald Welte3771d092010-04-30 20:26:32 +0200574 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200575}
576
577void gprs_ns_destroy(struct gprs_ns_inst *nsi)
578{
579 /* FIXME: clear all timers */
580
581 /* recursively free the NSI and all its NSVCs */
582 talloc_free(nsi);
583}
584
585
586/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
587 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
588
589/* Read a single NS-over-IP message */
590static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
591 struct sockaddr_in *saddr)
592{
593 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
594 int ret = 0;
595 socklen_t saddr_len = sizeof(*saddr);
596
597 if (!msg) {
598 *error = -ENOMEM;
599 return NULL;
600 }
601
602 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
603 (struct sockaddr *)saddr, &saddr_len);
604 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200605 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200606 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200607 msgb_free(msg);
608 *error = ret;
609 return NULL;
610 } else if (ret == 0) {
611 msgb_free(msg);
612 *error = ret;
613 return NULL;
614 }
615
616 msg->l2h = msg->data;
617 msgb_put(msg, ret);
618
619 return msg;
620}
621
622static int handle_nsip_read(struct bsc_fd *bfd)
623{
624 int error;
625 struct sockaddr_in saddr;
626 struct gprs_ns_inst *nsi = bfd->data;
627 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
628
629 if (!msg)
630 return error;
631
632 return gprs_ns_rcvmsg(nsi, msg, &saddr);
633}
634
635static int handle_nsip_write(struct bsc_fd *bfd)
636{
637 /* FIXME: actually send the data here instead of nsip_sendmsg() */
638 return -EIO;
639}
640
Harald Welte24a655f2010-04-30 19:54:29 +0200641int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200642{
643 int rc;
644 struct gprs_ns_inst *nsi = nsvc->nsi;
645 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
646
647 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
648 (struct sockaddr *)daddr, sizeof(*daddr));
649
650 talloc_free(msg);
651
652 return rc;
653}
654
655/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
656static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
657{
658 int rc = 0;
659
660 if (what & BSC_FD_READ)
661 rc = handle_nsip_read(bfd);
662 if (what & BSC_FD_WRITE)
663 rc = handle_nsip_write(bfd);
664
665 return rc;
666}
667
668
669/* FIXME: this is currently in input/ipaccess.c */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200670extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltef030b212010-04-26 19:18:54 +0200671 int (*cb)(struct bsc_fd *fd, unsigned int what));
672
673/* Listen for incoming GPRS packets */
674int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
675{
676 int ret;
677
678 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
679 if (ret < 0)
680 return ret;
681
682 nsi->ll = GPRS_NS_LL_UDP;
683 nsi->nsip.fd.data = nsi;
684
685 return ret;
686}
Harald Welte3771d092010-04-30 20:26:32 +0200687
688/* Establish a connection (from the BSS) to the SGSN */
689struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200690 struct sockaddr_in *dest, uint16_t nsei,
691 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200692{
693 struct gprs_nsvc *nsvc;
694
695 nsvc = nsvc_by_rem_addr(nsi, dest);
696 if (!nsvc) {
697 nsvc = nsvc_create(nsi, nsvci);
698 nsvc->ip.bts_addr = *dest;
699 }
Harald Welte1203de32010-05-01 11:28:43 +0200700 nsvc->nsei = nsei;
701 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200702 nsvc->remote_end_is_sgsn = 1;
703
704 /* Initiate a RESET procedure */
Harald Welte570fb832010-05-03 21:05:24 +0200705 if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200706 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
Harald Welte69a4cf22010-05-03 20:55:10 +0200707 nsei);
708 }
709 /* run a timer and re-transmit the reset request? */
710 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte3771d092010-04-30 20:26:32 +0200711
712 return nsvc;
713}