blob: 3d05dd0ff7aa8026f05490ae31ef15f74aa42dd1 [file] [log] [blame]
Harald Welte44f1c272010-04-30 19:54:29 +02001/* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg);
Harald Welte9b455bf2010-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 Weltecb991632010-04-26 19:18:54 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9b455bf2010-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 Welteeaa614c2010-05-02 11:26:34 +020050#include <stdint.h>
Harald Welte9b455bf2010-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 Weltecb991632010-04-26 19:18:54 +020058#include <osmocore/select.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080059#include <openbsc/debug.h>
Harald Welte99e32482010-05-11 06:20:54 +020060#include <openbsc/signal.h>
Harald Welte9b455bf2010-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 Weltecb991632010-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 Welteeaa614c2010-05-02 11:26:34 +020078 uint16_t nsvci)
Harald Weltecb991632010-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 Welte44f1c272010-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 Welteeaa614c2010-05-02 11:26:34 +020090 uint16_t nsei)
Harald Welte44f1c272010-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 Weltecb991632010-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 Welte199d9df2010-05-03 20:55:10 +0200113static void gprs_ns_timer_cb(void *data);
114
Harald Welteeaa614c2010-05-02 11:26:34 +0200115static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltecb991632010-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 Welte199d9df2010-05-03 20:55:10 +0200124 nsvc->timer.cb = gprs_ns_timer_cb;
125 nsvc->timer.data = nsvc;
126
Harald Weltecb991632010-04-26 19:18:54 +0200127 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
128
129 return nsvc;
130}
Harald Welte9b455bf2010-03-14 15:45:01 +0800131
Harald Welte1389ac72010-05-11 10:15:26 +0200132static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte99e32482010-05-11 06:20:54 +0200133 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 Welte9b455bf2010-03-14 15:45:01 +0800143/* Section 10.3.2, Table 13 */
Harald Welte345223e2010-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 Welte199d9df2010-05-03 20:55:10 +0200153 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte345223e2010-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 Welte9b455bf2010-03-14 15:45:01 +0800157};
158
Harald Welte345223e2010-05-02 09:37:45 +0200159const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800160{
Harald Welte345223e2010-05-02 09:37:45 +0200161 return get_value_string(ns_cause_str, cause);
Harald Welte9b455bf2010-03-14 15:45:01 +0800162}
163
Harald Welte44f1c272010-04-30 19:54:29 +0200164static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltecb991632010-04-26 19:18:54 +0200165
Harald Welte44f1c272010-04-30 19:54:29 +0200166static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800167{
Harald Weltecb991632010-04-26 19:18:54 +0200168 int ret;
169
170 switch (nsvc->nsi->ll) {
171 case GPRS_NS_LL_UDP:
Harald Welte44f1c272010-04-30 19:54:29 +0200172 ret = nsip_sendmsg(nsvc, msg);
Harald Weltecb991632010-04-26 19:18:54 +0200173 break;
174 default:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200175 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
Harald Weltecb991632010-04-26 19:18:54 +0200176 msgb_free(msg);
177 ret = -EIO;
178 break;
179 }
180 return ret;
Harald Welte9b455bf2010-03-14 15:45:01 +0800181}
182
Harald Welteeaa614c2010-05-02 11:26:34 +0200183static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9b455bf2010-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 Welte44f1c272010-04-30 19:54:29 +0200195 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800196}
197
Harald Welte99e32482010-05-11 06:20:54 +0200198int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte199d9df2010-05-03 20:55:10 +0200199{
200 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
201 struct gprs_ns_hdr *nsh;
Harald Welte199d9df2010-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 Welte99e32482010-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 Welte9b455bf2010-03-14 15:45:01 +0800245#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
246
Harald Welte199d9df2010-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 Welte05b320a2010-05-03 20:16:13 +0200264static void gprs_ns_timer_cb(void *data)
Harald Welte9b455bf2010-03-14 15:45:01 +0800265{
266 struct gprs_nsvc *nsvc = data;
267
Harald Welte05b320a2010-05-03 20:16:13 +0200268 switch (nsvc->timer_mode) {
269 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9b455bf2010-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 Welte6b72cdf2010-05-11 05:54:22 +0200275 DEBUGP(DNS, "NSEI=%u Tns-alive expired more then "
Harald Welte199d9df2010-05-03 20:55:10 +0200276 "%u times, blocking NS-VC\n", nsvc->nsei,
277 NS_ALIVE_RETRIES);
Harald Welte9b455bf2010-03-14 15:45:01 +0800278 /* FIXME: inform higher layers */
279 return;
280 }
Harald Welte199d9df2010-05-03 20:55:10 +0200281 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte05b320a2010-05-03 20:16:13 +0200282 break;
283 case NSVC_TIMER_TNS_TEST:
Harald Welte9b455bf2010-03-14 15:45:01 +0800284 /* Tns-test case: send NS-ALIVE PDU */
Harald Welte5434d7e2010-03-18 00:01:43 +0800285 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Welte9b455bf2010-03-14 15:45:01 +0800286 /* start Tns-alive timer */
Harald Welte199d9df2010-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 Welte90de93e2010-05-03 21:05:24 +0200291 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Welte199d9df2010-05-03 20:55:10 +0200292 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte05b320a2010-05-03 20:16:13 +0200293 break;
Harald Welte1389ac72010-05-11 10:15:26 +0200294 case _NSVC_TIMER_NR:
295 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800296 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800297}
298
299/* Section 9.2.6 */
Harald Welte5434d7e2010-03-18 00:01:43 +0800300static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9b455bf2010-03-14 15:45:01 +0800301{
302 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
303 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200304 uint16_t nsvci, nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800305
306 if (!msg)
307 return -ENOMEM;
308
Harald Welte5434d7e2010-03-18 00:01:43 +0800309 nsvci = htons(nsvc->nsvci);
310 nsei = htons(nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800311
312 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
313
314 nsh->pdu_type = NS_PDUT_RESET_ACK;
315
Harald Welte6b72cdf2010-05-11 05:54:22 +0200316 DEBUGP(DNS, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200317 nsvc->nsei, nsvc->nsvci);
Harald Welte5434d7e2010-03-18 00:01:43 +0800318
Harald Welteeaa614c2010-05-02 11:26:34 +0200319 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
320 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800321
Harald Welte44f1c272010-04-30 19:54:29 +0200322 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800323}
324
Harald Welte44f1c272010-04-30 19:54:29 +0200325/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
326int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800327{
Harald Welte44f1c272010-04-30 19:54:29 +0200328 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800329 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200330 uint16_t bvci = msgb_bvci(msg);
Harald Welte44f1c272010-04-30 19:54:29 +0200331
332 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
333 if (!nsvc) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200334 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Welte239cf052010-05-03 18:54:12 +0200335 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte44f1c272010-04-30 19:54:29 +0200336 return -EINVAL;
337 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800338
Harald Welte199d9df2010-05-03 20:55:10 +0200339 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200340 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200341 nsvc->nsei);
342 return -EBUSY;
343 }
344 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200345 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200346 nsvc->nsei);
347 return -EBUSY;
348 }
349
Harald Welte9b455bf2010-03-14 15:45:01 +0800350 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
351 if (!nsh) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200352 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800353 return -EIO;
354 }
355
356 nsh->pdu_type = NS_PDUT_UNITDATA;
357 /* spare octet in data[0] */
358 nsh->data[1] = bvci >> 8;
359 nsh->data[2] = bvci & 0xff;
360
Harald Welte44f1c272010-04-30 19:54:29 +0200361 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800362}
363
364/* Section 9.2.10: receive side */
Harald Welte44f1c272010-04-30 19:54:29 +0200365static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800366{
367 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welteeaa614c2010-05-02 11:26:34 +0200368 uint16_t bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800369
370 /* spare octet in data[0] */
371 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200372 msgb_bssgph(msg) = &nsh->data[3];
Harald Weltee6afd602010-05-02 11:19:37 +0200373 msgb_bvci(msg) = bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800374
375 /* call upper layer (BSSGP) */
Harald Weltecb991632010-04-26 19:18:54 +0200376 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800377}
378
379/* Section 9.2.7 */
Harald Welte44f1c272010-04-30 19:54:29 +0200380static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800381{
Harald Weltecb991632010-04-26 19:18:54 +0200382 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800383 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200384 uint8_t cause;
Harald Welte9b455bf2010-03-14 15:45:01 +0800385 int rc;
386
Harald Welte6b72cdf2010-05-11 05:54:22 +0200387 DEBUGP(DNS, "NSEI=%u NS STATUS ", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800388
389 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
390
391 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200392 DEBUGPC(DNS, "missing cause IE\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800393 return -EINVAL;
394 }
395
396 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte6b72cdf2010-05-11 05:54:22 +0200397 DEBUGPC(DNS, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800398
399 return 0;
400}
401
402/* Section 7.3 */
Harald Welte44f1c272010-04-30 19:54:29 +0200403static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800404{
405 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800406 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200407 uint8_t *cause;
408 uint16_t *nsvci, *nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800409 int rc;
410
Harald Welte9b455bf2010-03-14 15:45:01 +0800411 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
412
413 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
414 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
415 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
416 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200417 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800418 return -EINVAL;
419 }
420
Harald Welteeaa614c2010-05-02 11:26:34 +0200421 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
422 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
423 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9b455bf2010-03-14 15:45:01 +0800424
Harald Welted7c02ad2010-05-11 18:18:31 +0200425 DEBUGP(DNS, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200426 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
427
Harald Welte5434d7e2010-03-18 00:01:43 +0800428 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welted7c02ad2010-05-11 18:18:31 +0200429 /* FIXME: Check if we have an existing peer with this NSEI/NSVCI
430 * and remove it, as our BSS may just have changed its source IP
431 * address */
432
Harald Welte5434d7e2010-03-18 00:01:43 +0800433 nsvc->nsei = ntohs(*nsei);
434 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800435
Harald Welte9b455bf2010-03-14 15:45:01 +0800436 /* mark the NS-VC as blocked and alive */
Harald Welte9b455bf2010-03-14 15:45:01 +0800437 /* start the test procedure */
Harald Welte199d9df2010-05-03 20:55:10 +0200438 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9b455bf2010-03-14 15:45:01 +0800439
Harald Welte99e32482010-05-11 06:20:54 +0200440 /* inform interested parties about the fact that this NSVC
441 * has received RESET */
Harald Welte1389ac72010-05-11 10:15:26 +0200442 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200443
Harald Welte5434d7e2010-03-18 00:01:43 +0800444 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800445}
446
Harald Welte99e32482010-05-11 06:20:54 +0200447static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
448{
449 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
450 struct tlv_parsed tp;
451 uint8_t *cause;
452 int rc;
453
454 DEBUGP(DNS, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
455
456 nsvc->state |= NSE_S_BLOCKED;
457
458 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
459
460 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
461 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
462 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
463 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
464 return -EINVAL;
465 }
466
467 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
468 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
469
Harald Welte1389ac72010-05-11 10:15:26 +0200470 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200471
472 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
473}
474
Harald Welte9b455bf2010-03-14 15:45:01 +0800475/* main entry point, here incoming NS frames enter */
Harald Weltecb991632010-04-26 19:18:54 +0200476int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
477 struct sockaddr_in *saddr)
Harald Welte9b455bf2010-03-14 15:45:01 +0800478{
479 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltecb991632010-04-26 19:18:54 +0200480 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800481 int rc = 0;
482
Harald Weltecb991632010-04-26 19:18:54 +0200483 /* look up the NSVC based on source address */
484 nsvc = nsvc_by_rem_addr(nsi, saddr);
485 if (!nsvc) {
Harald Weltee5117da2010-05-11 18:30:37 +0200486 struct tlv_parsed tp;
487 uint16_t nsei;
Harald Weltecb991632010-04-26 19:18:54 +0200488 /* Only the RESET procedure creates a new NSVC */
Harald Welte239cf052010-05-03 18:54:12 +0200489 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200490 LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x "
Harald Welte239cf052010-05-03 18:54:12 +0200491 "from %s for non-existing NS-VC\n",
492 nsh->pdu_type, inet_ntoa(saddr->sin_addr));
Harald Welte90de93e2010-05-03 21:05:24 +0200493 //gprs_ns_tx_reset(nsvc, NS_CAUSE_NSVC_UNKNOWN);
Harald Weltecb991632010-04-26 19:18:54 +0200494 return -EIO;
Harald Welte239cf052010-05-03 18:54:12 +0200495 }
Harald Weltee5117da2010-05-11 18:30:37 +0200496 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
497 msgb_l2len(msg), 0, 0);
498 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
499 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
500 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
501 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
502 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
503 return -EINVAL;
504 }
505 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
506 /* Check if we already know this NSEI, the remote end might
507 * simply have changed addresses, or it is a SGSN */
508 nsvc = nsvc_by_nsei(nsi, nsei);
509 if (!nsvc) {
510 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
511 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
512 nsvc = nsvc_create(nsi, 0xffff);
513 nsvc->ip.bts_addr = *saddr;
514 }
Harald Welte239cf052010-05-03 18:54:12 +0200515 } else
516 msgb_nsei(msg) = nsvc->nsei;
Harald Welte5434d7e2010-03-18 00:01:43 +0800517
Harald Welte9b455bf2010-03-14 15:45:01 +0800518 switch (nsh->pdu_type) {
519 case NS_PDUT_ALIVE:
520 /* remote end inquires whether we're still alive,
521 * we need to respond with ALIVE_ACK */
Harald Welte5434d7e2010-03-18 00:01:43 +0800522 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
Harald Welte9b455bf2010-03-14 15:45:01 +0800523 break;
524 case NS_PDUT_ALIVE_ACK:
525 /* stop Tns-alive */
Harald Welte05b320a2010-05-03 20:16:13 +0200526 bsc_del_timer(&nsvc->timer);
Harald Welte9b455bf2010-03-14 15:45:01 +0800527 /* start Tns-test */
Harald Welte199d9df2010-05-03 20:55:10 +0200528 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte24b31312010-05-03 21:11:22 +0200529 if (nsvc->remote_end_is_sgsn) {
530 /* FIXME: this should be one level higher */
531 if (nsvc->state & NSE_S_BLOCKED)
532 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
533 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800534 break;
535 case NS_PDUT_UNITDATA:
536 /* actual user data */
Harald Welte44f1c272010-04-30 19:54:29 +0200537 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800538 break;
539 case NS_PDUT_STATUS:
Harald Welte44f1c272010-04-30 19:54:29 +0200540 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800541 break;
542 case NS_PDUT_RESET:
Harald Welte44f1c272010-04-30 19:54:29 +0200543 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800544 break;
545 case NS_PDUT_RESET_ACK:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200546 DEBUGP(DNS, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltecb991632010-04-26 19:18:54 +0200547 /* mark remote NS-VC as blocked + active */
548 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte199d9df2010-05-03 20:55:10 +0200549 if (nsvc->remote_end_is_sgsn) {
550 /* stop RESET timer */
551 bsc_del_timer(&nsvc->timer);
Harald Welte90de93e2010-05-03 21:05:24 +0200552 /* send ALIVE PDU */
553 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
554 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte24b31312010-05-03 21:11:22 +0200555 /* mark local state as BLOCKED + ALIVE */
556 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte199d9df2010-05-03 20:55:10 +0200557 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800558 break;
559 case NS_PDUT_UNBLOCK:
560 /* Section 7.2: unblocking procedure */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200561 DEBUGP(DNS, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800562 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte99e32482010-05-11 06:20:54 +0200563 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte5434d7e2010-03-18 00:01:43 +0800564 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9b455bf2010-03-14 15:45:01 +0800565 break;
566 case NS_PDUT_UNBLOCK_ACK:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200567 DEBUGP(DNS, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltecb991632010-04-26 19:18:54 +0200568 /* mark remote NS-VC as unblocked + active */
569 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte24b31312010-05-03 21:11:22 +0200570 if (nsvc->remote_end_is_sgsn)
571 nsvc->state = NSE_S_ALIVE;
Harald Welte9b455bf2010-03-14 15:45:01 +0800572 break;
573 case NS_PDUT_BLOCK:
Harald Welte99e32482010-05-11 06:20:54 +0200574 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800575 break;
576 case NS_PDUT_BLOCK_ACK:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200577 DEBUGP(DNS, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltecb991632010-04-26 19:18:54 +0200578 /* mark remote NS-VC as blocked + active */
579 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9b455bf2010-03-14 15:45:01 +0800580 break;
581 default:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200582 DEBUGP(DNS, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Welte239cf052010-05-03 18:54:12 +0200583 nsvc->nsei, nsh->pdu_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800584 rc = -EINVAL;
585 break;
586 }
587 return rc;
588}
589
Harald Weltecb991632010-04-26 19:18:54 +0200590struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
591{
592 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
593
594 nsi->cb = cb;
595 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
596
Harald Welte9f75c352010-04-30 20:26:32 +0200597 return nsi;
Harald Weltecb991632010-04-26 19:18:54 +0200598}
599
600void gprs_ns_destroy(struct gprs_ns_inst *nsi)
601{
602 /* FIXME: clear all timers */
603
604 /* recursively free the NSI and all its NSVCs */
605 talloc_free(nsi);
606}
607
608
609/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
610 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
611
612/* Read a single NS-over-IP message */
613static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
614 struct sockaddr_in *saddr)
615{
616 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
617 int ret = 0;
618 socklen_t saddr_len = sizeof(*saddr);
619
620 if (!msg) {
621 *error = -ENOMEM;
622 return NULL;
623 }
624
625 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
626 (struct sockaddr *)saddr, &saddr_len);
627 if (ret < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200628 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Welte239cf052010-05-03 18:54:12 +0200629 strerror(errno));
Harald Weltecb991632010-04-26 19:18:54 +0200630 msgb_free(msg);
631 *error = ret;
632 return NULL;
633 } else if (ret == 0) {
634 msgb_free(msg);
635 *error = ret;
636 return NULL;
637 }
638
639 msg->l2h = msg->data;
640 msgb_put(msg, ret);
641
642 return msg;
643}
644
645static int handle_nsip_read(struct bsc_fd *bfd)
646{
647 int error;
648 struct sockaddr_in saddr;
649 struct gprs_ns_inst *nsi = bfd->data;
650 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
651
652 if (!msg)
653 return error;
654
655 return gprs_ns_rcvmsg(nsi, msg, &saddr);
656}
657
658static int handle_nsip_write(struct bsc_fd *bfd)
659{
660 /* FIXME: actually send the data here instead of nsip_sendmsg() */
661 return -EIO;
662}
663
Harald Welte44f1c272010-04-30 19:54:29 +0200664int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltecb991632010-04-26 19:18:54 +0200665{
666 int rc;
667 struct gprs_ns_inst *nsi = nsvc->nsi;
668 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
669
670 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
671 (struct sockaddr *)daddr, sizeof(*daddr));
672
673 talloc_free(msg);
674
675 return rc;
676}
677
678/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
679static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
680{
681 int rc = 0;
682
683 if (what & BSC_FD_READ)
684 rc = handle_nsip_read(bfd);
685 if (what & BSC_FD_WRITE)
686 rc = handle_nsip_write(bfd);
687
688 return rc;
689}
690
691
692/* FIXME: this is currently in input/ipaccess.c */
Harald Welteeaa614c2010-05-02 11:26:34 +0200693extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltecb991632010-04-26 19:18:54 +0200694 int (*cb)(struct bsc_fd *fd, unsigned int what));
695
696/* Listen for incoming GPRS packets */
697int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
698{
699 int ret;
700
701 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
702 if (ret < 0)
703 return ret;
704
705 nsi->ll = GPRS_NS_LL_UDP;
706 nsi->nsip.fd.data = nsi;
707
708 return ret;
709}
Harald Welte9f75c352010-04-30 20:26:32 +0200710
711/* Establish a connection (from the BSS) to the SGSN */
712struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welteb77c6972010-05-01 11:28:43 +0200713 struct sockaddr_in *dest, uint16_t nsei,
714 uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200715{
716 struct gprs_nsvc *nsvc;
717
718 nsvc = nsvc_by_rem_addr(nsi, dest);
719 if (!nsvc) {
720 nsvc = nsvc_create(nsi, nsvci);
721 nsvc->ip.bts_addr = *dest;
722 }
Harald Welteb77c6972010-05-01 11:28:43 +0200723 nsvc->nsei = nsei;
724 nsvc->nsvci = nsvci;
Harald Welte9f75c352010-04-30 20:26:32 +0200725 nsvc->remote_end_is_sgsn = 1;
726
727 /* Initiate a RESET procedure */
Harald Welte90de93e2010-05-03 21:05:24 +0200728 if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200729 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200730 nsei);
731 }
732 /* run a timer and re-transmit the reset request? */
733 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte9f75c352010-04-30 20:26:32 +0200734
735 return nsvc;
736}