blob: ed64210328fae12f2245de30c74cdc578cfb37cf [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) {
Harald Weltecad83012010-05-12 11:56:39 +0000107 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
108 sin->sin_addr.s_addr &&
109 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltecb991632010-04-26 19:18:54 +0200110 return nsvc;
111 }
112 return NULL;
113}
114
Harald Welte199d9df2010-05-03 20:55:10 +0200115static void gprs_ns_timer_cb(void *data);
116
Harald Welteeaa614c2010-05-02 11:26:34 +0200117static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltecb991632010-04-26 19:18:54 +0200118{
119 struct gprs_nsvc *nsvc;
120
121 nsvc = talloc_zero(nsi, struct gprs_nsvc);
122 nsvc->nsvci = nsvci;
123 /* before RESET procedure: BLOCKED and DEAD */
124 nsvc->state = NSE_S_BLOCKED;
125 nsvc->nsi = nsi;
Harald Welte199d9df2010-05-03 20:55:10 +0200126 nsvc->timer.cb = gprs_ns_timer_cb;
127 nsvc->timer.data = nsvc;
128
Harald Weltecb991632010-04-26 19:18:54 +0200129 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
130
131 return nsvc;
132}
Harald Welte9b455bf2010-03-14 15:45:01 +0800133
Harald Welte1194b582010-05-12 15:55:23 +0000134static void nsvc_delete(struct gprs_nsvc *nsvc)
135{
136 if (bsc_timer_pending(&nsvc->timer))
137 bsc_del_timer(&nsvc->timer);
138 llist_del(&nsvc->list);
139 talloc_free(nsvc);
140}
141
Harald Welte1389ac72010-05-11 10:15:26 +0200142static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte99e32482010-05-11 06:20:54 +0200143 uint8_t cause)
144{
145 struct ns_signal_data nssd;
146
147 nssd.nsvc = nsvc;
148 nssd.cause = cause;
149
150 dispatch_signal(SS_NS, signal, &nssd);
151}
152
Harald Welte9b455bf2010-03-14 15:45:01 +0800153/* Section 10.3.2, Table 13 */
Harald Welte345223e2010-05-02 09:37:45 +0200154static const struct value_string ns_cause_str[] = {
155 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
156 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
157 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
158 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
159 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
160 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
161 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
162 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte199d9df2010-05-03 20:55:10 +0200163 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte345223e2010-05-02 09:37:45 +0200164 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
165 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
166 { 0, NULL }
Harald Welte9b455bf2010-03-14 15:45:01 +0800167};
168
Harald Welte345223e2010-05-02 09:37:45 +0200169const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800170{
Harald Welte345223e2010-05-02 09:37:45 +0200171 return get_value_string(ns_cause_str, cause);
Harald Welte9b455bf2010-03-14 15:45:01 +0800172}
173
Harald Welte44f1c272010-04-30 19:54:29 +0200174static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltecb991632010-04-26 19:18:54 +0200175
Harald Welte44f1c272010-04-30 19:54:29 +0200176static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800177{
Harald Weltecb991632010-04-26 19:18:54 +0200178 int ret;
179
180 switch (nsvc->nsi->ll) {
181 case GPRS_NS_LL_UDP:
Harald Welte44f1c272010-04-30 19:54:29 +0200182 ret = nsip_sendmsg(nsvc, msg);
Harald Weltecb991632010-04-26 19:18:54 +0200183 break;
184 default:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200185 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
Harald Weltecb991632010-04-26 19:18:54 +0200186 msgb_free(msg);
187 ret = -EIO;
188 break;
189 }
190 return ret;
Harald Welte9b455bf2010-03-14 15:45:01 +0800191}
192
Harald Welteeaa614c2010-05-02 11:26:34 +0200193static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800194{
195 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
196 struct gprs_ns_hdr *nsh;
197
198 if (!msg)
199 return -ENOMEM;
200
201 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
202
203 nsh->pdu_type = pdu_type;
204
Harald Welte44f1c272010-04-30 19:54:29 +0200205 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800206}
207
Harald Welte99e32482010-05-11 06:20:54 +0200208int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte199d9df2010-05-03 20:55:10 +0200209{
210 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
211 struct gprs_ns_hdr *nsh;
Harald Welte199d9df2010-05-03 20:55:10 +0200212 uint16_t nsvci = htons(nsvc->nsvci);
213 uint16_t nsei = htons(nsvc->nsei);
214
215 if (!msg)
216 return -ENOMEM;
217
Harald Welte34caeb32010-05-12 12:01:33 +0000218 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
219 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
220
Harald Welte199d9df2010-05-03 20:55:10 +0200221 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
222 nsh->pdu_type = NS_PDUT_RESET;
223
224 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
225 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
226 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
227
228 return gprs_ns_tx(nsvc, msg);
229
230}
231
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200232int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
233 uint16_t bvci, struct msgb *orig_msg)
234{
235 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
236 struct gprs_ns_hdr *nsh;
237 uint16_t nsvci = htons(nsvc->nsvci);
238
239 bvci = htons(bvci);
240
241 if (!msg)
242 return -ENOMEM;
243
Harald Welte34caeb32010-05-12 12:01:33 +0000244 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
245 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
246
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200247 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
248 nsh->pdu_type = NS_PDUT_STATUS;
249
250 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
251
252 /* Section 9.2.7.1: Static conditions for NS-VCI */
253 if (cause == NS_CAUSE_NSVC_BLOCKED ||
254 cause == NS_CAUSE_NSVC_UNKNOWN)
255 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
256
257 /* Section 9.2.7.2: Static conditions for NS PDU */
258 switch (cause) {
259 case NS_CAUSE_SEM_INCORR_PDU:
260 case NS_CAUSE_PDU_INCOMP_PSTATE:
261 case NS_CAUSE_PROTO_ERR_UNSPEC:
262 case NS_CAUSE_INVAL_ESSENT_IE:
263 case NS_CAUSE_MISSING_ESSENT_IE:
264 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
265 orig_msg->l2h);
266 break;
267 default:
268 break;
269 }
270
271 /* Section 9.2.7.3: Static conditions for BVCI */
272 if (cause == NS_CAUSE_BVCI_UNKNOWN)
273 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
274
275 return gprs_ns_tx(nsvc, msg);
276}
277
Harald Welte99e32482010-05-11 06:20:54 +0200278int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
279{
280 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
281 struct gprs_ns_hdr *nsh;
282 uint16_t nsvci = htons(nsvc->nsvci);
283
284 if (!msg)
285 return -ENOMEM;
286
Harald Welte34caeb32010-05-12 12:01:33 +0000287 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
288 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
289
Harald Welte99e32482010-05-11 06:20:54 +0200290 /* be conservative and mark it as blocked even now! */
291 nsvc->state |= NSE_S_BLOCKED;
292
293 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
294 nsh->pdu_type = NS_PDUT_BLOCK;
295
296 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
297 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
298
299 return gprs_ns_tx(nsvc, msg);
300}
301
302int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
303{
Harald Welte34caeb32010-05-12 12:01:33 +0000304 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
305 nsvc->nsei, nsvc->nsvci);
306
Harald Welte99e32482010-05-11 06:20:54 +0200307 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
308}
309
Harald Weltebca900d2010-05-12 12:11:45 +0000310int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
311{
312 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
313 nsvc->nsei, nsvc->nsvci);
314
315 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
316}
317
318int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
319{
320 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
321 nsvc->nsei, nsvc->nsvci);
322
323 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
324}
325
Harald Welte9b455bf2010-03-14 15:45:01 +0800326#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
327
Harald Welte199d9df2010-05-03 20:55:10 +0200328static const uint8_t timer_mode_tout[_NSVC_TIMER_NR] = {
329 [NSVC_TIMER_TNS_RESET] = 60,
330 [NSVC_TIMER_TNS_ALIVE] = 3,
331 [NSVC_TIMER_TNS_TEST] = 30,
332};
333
Harald Welte811c4972010-05-12 12:21:52 +0000334static const struct value_string timer_mode_strs[] = {
335 { NSVC_TIMER_TNS_RESET, "tns-reset" },
336 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
337 { NSVC_TIMER_TNS_TEST, "tns-test" },
338 { 0, NULL }
339};
340
Harald Welte199d9df2010-05-03 20:55:10 +0200341static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
342{
Harald Welted4eaf802010-05-12 13:55:36 +0000343 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
344 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Welte811c4972010-05-12 12:21:52 +0000345 timer_mode_tout[mode]);
346
Harald Welte199d9df2010-05-03 20:55:10 +0200347 if (bsc_timer_pending(&nsvc->timer))
348 bsc_del_timer(&nsvc->timer);
349
350 nsvc->timer_mode = mode;
351 bsc_schedule_timer(&nsvc->timer, timer_mode_tout[mode], 0);
352}
353
Harald Welte05b320a2010-05-03 20:16:13 +0200354static void gprs_ns_timer_cb(void *data)
Harald Welte9b455bf2010-03-14 15:45:01 +0800355{
356 struct gprs_nsvc *nsvc = data;
357
Harald Welted4eaf802010-05-12 13:55:36 +0000358 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
359 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Welte811c4972010-05-12 12:21:52 +0000360 timer_mode_tout[nsvc->timer_mode]);
361
Harald Welte05b320a2010-05-03 20:16:13 +0200362 switch (nsvc->timer_mode) {
363 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9b455bf2010-03-14 15:45:01 +0800364 /* Tns-alive case: we expired without response ! */
365 nsvc->alive_retries++;
366 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
367 /* mark as dead and blocked */
368 nsvc->state = NSE_S_BLOCKED;
Harald Weltefcc4cc92010-05-12 00:16:57 +0200369 LOGP(DNS, LOGL_NOTICE,
370 "NSEI=%u Tns-alive expired more then "
Harald Welte199d9df2010-05-03 20:55:10 +0200371 "%u times, blocking NS-VC\n", nsvc->nsei,
372 NS_ALIVE_RETRIES);
Harald Welteb778d2c2010-05-12 13:28:25 +0000373 ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200374 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800375 return;
376 }
Harald Weltebca900d2010-05-12 12:11:45 +0000377 /* Tns-test case: send NS-ALIVE PDU */
378 gprs_ns_tx_alive(nsvc);
379 /* start Tns-alive timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200380 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte05b320a2010-05-03 20:16:13 +0200381 break;
382 case NSVC_TIMER_TNS_TEST:
Harald Welte9b455bf2010-03-14 15:45:01 +0800383 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltebca900d2010-05-12 12:11:45 +0000384 gprs_ns_tx_alive(nsvc);
385 /* start Tns-alive timer (transition into faster
386 * alive retransmissions) */
Harald Welte811c4972010-05-12 12:21:52 +0000387 nsvc->alive_retries = 0;
Harald Welte199d9df2010-05-03 20:55:10 +0200388 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
389 break;
390 case NSVC_TIMER_TNS_RESET:
391 /* Chapter 7.3: Re-send the RESET */
Harald Welte90de93e2010-05-03 21:05:24 +0200392 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200393 /* Re-start Tns-reset timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200394 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte05b320a2010-05-03 20:16:13 +0200395 break;
Harald Welte1389ac72010-05-11 10:15:26 +0200396 case _NSVC_TIMER_NR:
397 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800398 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800399}
400
401/* Section 9.2.6 */
Harald Welte5434d7e2010-03-18 00:01:43 +0800402static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9b455bf2010-03-14 15:45:01 +0800403{
404 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
405 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200406 uint16_t nsvci, nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800407
408 if (!msg)
409 return -ENOMEM;
410
Harald Welte5434d7e2010-03-18 00:01:43 +0800411 nsvci = htons(nsvc->nsvci);
412 nsei = htons(nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800413
414 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
415
416 nsh->pdu_type = NS_PDUT_RESET_ACK;
417
Harald Weltefcc4cc92010-05-12 00:16:57 +0200418 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200419 nsvc->nsei, nsvc->nsvci);
Harald Welte5434d7e2010-03-18 00:01:43 +0800420
Harald Welteeaa614c2010-05-02 11:26:34 +0200421 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
422 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800423
Harald Welte44f1c272010-04-30 19:54:29 +0200424 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800425}
426
Harald Welte44f1c272010-04-30 19:54:29 +0200427/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
428int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800429{
Harald Welte44f1c272010-04-30 19:54:29 +0200430 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800431 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200432 uint16_t bvci = msgb_bvci(msg);
Harald Welte44f1c272010-04-30 19:54:29 +0200433
434 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
435 if (!nsvc) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200436 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Welte239cf052010-05-03 18:54:12 +0200437 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte44f1c272010-04-30 19:54:29 +0200438 return -EINVAL;
439 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800440
Harald Welte199d9df2010-05-03 20:55:10 +0200441 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200442 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200443 nsvc->nsei);
444 return -EBUSY;
445 }
446 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200447 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200448 nsvc->nsei);
449 return -EBUSY;
450 }
451
Harald Welte9b455bf2010-03-14 15:45:01 +0800452 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
453 if (!nsh) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200454 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800455 return -EIO;
456 }
457
458 nsh->pdu_type = NS_PDUT_UNITDATA;
459 /* spare octet in data[0] */
460 nsh->data[1] = bvci >> 8;
461 nsh->data[2] = bvci & 0xff;
462
Harald Welte44f1c272010-04-30 19:54:29 +0200463 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800464}
465
466/* Section 9.2.10: receive side */
Harald Welte44f1c272010-04-30 19:54:29 +0200467static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800468{
469 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welteeaa614c2010-05-02 11:26:34 +0200470 uint16_t bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800471
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200472 if (nsvc->state & NSE_S_BLOCKED)
473 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
474 0, msg);
475
Harald Welte9b455bf2010-03-14 15:45:01 +0800476 /* spare octet in data[0] */
477 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200478 msgb_bssgph(msg) = &nsh->data[3];
Harald Weltee6afd602010-05-02 11:19:37 +0200479 msgb_bvci(msg) = bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800480
481 /* call upper layer (BSSGP) */
Harald Weltecb991632010-04-26 19:18:54 +0200482 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800483}
484
485/* Section 9.2.7 */
Harald Welte44f1c272010-04-30 19:54:29 +0200486static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800487{
Harald Weltecb991632010-04-26 19:18:54 +0200488 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800489 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200490 uint8_t cause;
Harald Welte9b455bf2010-03-14 15:45:01 +0800491 int rc;
492
Harald Weltefcc4cc92010-05-12 00:16:57 +0200493 LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800494
495 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
496
497 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltefcc4cc92010-05-12 00:16:57 +0200498 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800499 return -EINVAL;
500 }
501
502 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200503 LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800504
505 return 0;
506}
507
508/* Section 7.3 */
Harald Welte44f1c272010-04-30 19:54:29 +0200509static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800510{
511 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800512 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200513 uint8_t *cause;
514 uint16_t *nsvci, *nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800515 int rc;
516
Harald Welte9b455bf2010-03-14 15:45:01 +0800517 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
518
519 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
520 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
521 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200522 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200523 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800524 return -EINVAL;
525 }
526
Harald Welteeaa614c2010-05-02 11:26:34 +0200527 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
528 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
529 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9b455bf2010-03-14 15:45:01 +0800530
Harald Weltefcc4cc92010-05-12 00:16:57 +0200531 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200532 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
533
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200534 /* Mark NS-VC as blocked and alive */
Harald Welte5434d7e2010-03-18 00:01:43 +0800535 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welted7c02ad2010-05-11 18:18:31 +0200536
Harald Welte5434d7e2010-03-18 00:01:43 +0800537 nsvc->nsei = ntohs(*nsei);
538 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800539
Harald Welte9b455bf2010-03-14 15:45:01 +0800540 /* start the test procedure */
Harald Welte199d9df2010-05-03 20:55:10 +0200541 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9b455bf2010-03-14 15:45:01 +0800542
Harald Welte99e32482010-05-11 06:20:54 +0200543 /* inform interested parties about the fact that this NSVC
544 * has received RESET */
Harald Welte1389ac72010-05-11 10:15:26 +0200545 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200546
Harald Welte5434d7e2010-03-18 00:01:43 +0800547 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800548}
549
Harald Welte99e32482010-05-11 06:20:54 +0200550static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
551{
552 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
553 struct tlv_parsed tp;
554 uint8_t *cause;
555 int rc;
556
Harald Weltefcc4cc92010-05-12 00:16:57 +0200557 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte99e32482010-05-11 06:20:54 +0200558
559 nsvc->state |= NSE_S_BLOCKED;
560
561 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
562
563 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
564 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte99e32482010-05-11 06:20:54 +0200565 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200566 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte99e32482010-05-11 06:20:54 +0200567 return -EINVAL;
568 }
569
570 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
571 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
572
Harald Welte1389ac72010-05-11 10:15:26 +0200573 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200574
575 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
576}
577
Harald Welte9b455bf2010-03-14 15:45:01 +0800578/* main entry point, here incoming NS frames enter */
Harald Weltecb991632010-04-26 19:18:54 +0200579int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
580 struct sockaddr_in *saddr)
Harald Welte9b455bf2010-03-14 15:45:01 +0800581{
582 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltecb991632010-04-26 19:18:54 +0200583 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800584 int rc = 0;
585
Harald Weltecb991632010-04-26 19:18:54 +0200586 /* look up the NSVC based on source address */
587 nsvc = nsvc_by_rem_addr(nsi, saddr);
588 if (!nsvc) {
Harald Weltee5117da2010-05-11 18:30:37 +0200589 struct tlv_parsed tp;
590 uint16_t nsei;
Harald Weltecb991632010-04-26 19:18:54 +0200591 /* Only the RESET procedure creates a new NSVC */
Harald Welte239cf052010-05-03 18:54:12 +0200592 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welte8c2440e2010-05-12 13:51:08 +0000593 struct gprs_nsvc fake_nsvc;
Harald Welte6b72cdf2010-05-11 05:54:22 +0200594 LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x "
Harald Welteef1226e2010-05-11 18:38:36 +0200595 "from %s:%u for non-existing NS-VC\n",
596 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
597 ntohs(saddr->sin_port));
Harald Welte8c2440e2010-05-12 13:51:08 +0000598 /* Since we have no NSVC, we have to create a fake */
599 fake_nsvc.nsvci = fake_nsvc.nsei = 0;
600 fake_nsvc.nsi = nsi;
601 fake_nsvc.ip.bts_addr = *saddr;
602 fake_nsvc.state = NSE_S_ALIVE;
Harald Welte8c2440e2010-05-12 13:51:08 +0000603 return gprs_ns_tx_status(&fake_nsvc,
604 NS_CAUSE_PDU_INCOMP_PSTATE, 0,
605 msg);
Harald Welte239cf052010-05-03 18:54:12 +0200606 }
Harald Weltee5117da2010-05-11 18:30:37 +0200607 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
608 msgb_l2len(msg), 0, 0);
609 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
610 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
611 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Weltee5117da2010-05-11 18:30:37 +0200612 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200613 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
614 msg);
Harald Weltee5117da2010-05-11 18:30:37 +0200615 return -EINVAL;
616 }
617 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
618 /* Check if we already know this NSEI, the remote end might
619 * simply have changed addresses, or it is a SGSN */
620 nsvc = nsvc_by_nsei(nsi, nsei);
621 if (!nsvc) {
622 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
623 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
624 nsvc = nsvc_create(nsi, 0xffff);
Harald Weltee5117da2010-05-11 18:30:37 +0200625 }
Harald Welte7c209eb2010-05-11 18:40:45 +0200626 /* Update the remote peer IP address/port */
627 nsvc->ip.bts_addr = *saddr;
Harald Welte239cf052010-05-03 18:54:12 +0200628 } else
629 msgb_nsei(msg) = nsvc->nsei;
Harald Welte5434d7e2010-03-18 00:01:43 +0800630
Harald Welte9b455bf2010-03-14 15:45:01 +0800631 switch (nsh->pdu_type) {
632 case NS_PDUT_ALIVE:
Harald Welte1194b582010-05-12 15:55:23 +0000633 /* If we're dead and blocked and suddenly receive a
634 * NS-ALIVE out of the blue, we might have been re-started
635 * and should send a NS-RESET to make sure everything recovers
636 * fine. */
637 if (nsvc->state == NSE_S_BLOCKED)
638 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
639 else
640 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800641 break;
642 case NS_PDUT_ALIVE_ACK:
643 /* stop Tns-alive */
Harald Welte05b320a2010-05-03 20:16:13 +0200644 bsc_del_timer(&nsvc->timer);
Harald Welte9b455bf2010-03-14 15:45:01 +0800645 /* start Tns-test */
Harald Welte199d9df2010-05-03 20:55:10 +0200646 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte24b31312010-05-03 21:11:22 +0200647 if (nsvc->remote_end_is_sgsn) {
648 /* FIXME: this should be one level higher */
649 if (nsvc->state & NSE_S_BLOCKED)
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200650 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte24b31312010-05-03 21:11:22 +0200651 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800652 break;
653 case NS_PDUT_UNITDATA:
654 /* actual user data */
Harald Welte44f1c272010-04-30 19:54:29 +0200655 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800656 break;
657 case NS_PDUT_STATUS:
Harald Welte44f1c272010-04-30 19:54:29 +0200658 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800659 break;
660 case NS_PDUT_RESET:
Harald Welte44f1c272010-04-30 19:54:29 +0200661 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800662 break;
663 case NS_PDUT_RESET_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200664 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200665 /* mark NS-VC as blocked + active */
666 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200667 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte199d9df2010-05-03 20:55:10 +0200668 if (nsvc->remote_end_is_sgsn) {
669 /* stop RESET timer */
670 bsc_del_timer(&nsvc->timer);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200671 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte90de93e2010-05-03 21:05:24 +0200672 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
673 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte199d9df2010-05-03 20:55:10 +0200674 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800675 break;
676 case NS_PDUT_UNBLOCK:
677 /* Section 7.2: unblocking procedure */
Harald Weltefcc4cc92010-05-12 00:16:57 +0200678 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800679 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte99e32482010-05-11 06:20:54 +0200680 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte5434d7e2010-03-18 00:01:43 +0800681 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9b455bf2010-03-14 15:45:01 +0800682 break;
683 case NS_PDUT_UNBLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200684 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200685 /* mark NS-VC as unblocked + active */
686 nsvc->state = NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200687 nsvc->remote_state = NSE_S_ALIVE;
Harald Weltef6d67c02010-05-12 14:18:46 +0000688 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +0800689 break;
690 case NS_PDUT_BLOCK:
Harald Welte99e32482010-05-11 06:20:54 +0200691 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800692 break;
693 case NS_PDUT_BLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200694 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltecb991632010-04-26 19:18:54 +0200695 /* mark remote NS-VC as blocked + active */
696 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9b455bf2010-03-14 15:45:01 +0800697 break;
698 default:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200699 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Welte239cf052010-05-03 18:54:12 +0200700 nsvc->nsei, nsh->pdu_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800701 rc = -EINVAL;
702 break;
703 }
704 return rc;
705}
706
Harald Weltecb991632010-04-26 19:18:54 +0200707struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
708{
709 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
710
711 nsi->cb = cb;
712 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
713
Harald Welte9f75c352010-04-30 20:26:32 +0200714 return nsi;
Harald Weltecb991632010-04-26 19:18:54 +0200715}
716
717void gprs_ns_destroy(struct gprs_ns_inst *nsi)
718{
719 /* FIXME: clear all timers */
720
721 /* recursively free the NSI and all its NSVCs */
722 talloc_free(nsi);
723}
724
725
726/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
727 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
728
729/* Read a single NS-over-IP message */
730static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
731 struct sockaddr_in *saddr)
732{
733 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
734 int ret = 0;
735 socklen_t saddr_len = sizeof(*saddr);
736
737 if (!msg) {
738 *error = -ENOMEM;
739 return NULL;
740 }
741
742 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
743 (struct sockaddr *)saddr, &saddr_len);
744 if (ret < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200745 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Welte239cf052010-05-03 18:54:12 +0200746 strerror(errno));
Harald Weltecb991632010-04-26 19:18:54 +0200747 msgb_free(msg);
748 *error = ret;
749 return NULL;
750 } else if (ret == 0) {
751 msgb_free(msg);
752 *error = ret;
753 return NULL;
754 }
755
756 msg->l2h = msg->data;
757 msgb_put(msg, ret);
758
759 return msg;
760}
761
762static int handle_nsip_read(struct bsc_fd *bfd)
763{
764 int error;
765 struct sockaddr_in saddr;
766 struct gprs_ns_inst *nsi = bfd->data;
767 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
768
769 if (!msg)
770 return error;
771
772 return gprs_ns_rcvmsg(nsi, msg, &saddr);
773}
774
775static int handle_nsip_write(struct bsc_fd *bfd)
776{
777 /* FIXME: actually send the data here instead of nsip_sendmsg() */
778 return -EIO;
779}
780
Harald Welte44f1c272010-04-30 19:54:29 +0200781int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltecb991632010-04-26 19:18:54 +0200782{
783 int rc;
784 struct gprs_ns_inst *nsi = nsvc->nsi;
785 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
786
787 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
788 (struct sockaddr *)daddr, sizeof(*daddr));
789
790 talloc_free(msg);
791
792 return rc;
793}
794
795/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
796static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
797{
798 int rc = 0;
799
800 if (what & BSC_FD_READ)
801 rc = handle_nsip_read(bfd);
802 if (what & BSC_FD_WRITE)
803 rc = handle_nsip_write(bfd);
804
805 return rc;
806}
807
Harald Welteeaa614c2010-05-02 11:26:34 +0200808extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltecb991632010-04-26 19:18:54 +0200809 int (*cb)(struct bsc_fd *fd, unsigned int what));
810
811/* Listen for incoming GPRS packets */
812int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
813{
814 int ret;
815
816 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
817 if (ret < 0)
818 return ret;
819
820 nsi->ll = GPRS_NS_LL_UDP;
821 nsi->nsip.fd.data = nsi;
822
823 return ret;
824}
Harald Welte9f75c352010-04-30 20:26:32 +0200825
826/* Establish a connection (from the BSS) to the SGSN */
827struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welteb77c6972010-05-01 11:28:43 +0200828 struct sockaddr_in *dest, uint16_t nsei,
829 uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200830{
831 struct gprs_nsvc *nsvc;
832
833 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Weltecad83012010-05-12 11:56:39 +0000834 if (!nsvc)
Harald Welte9f75c352010-04-30 20:26:32 +0200835 nsvc = nsvc_create(nsi, nsvci);
Harald Weltecad83012010-05-12 11:56:39 +0000836 nsvc->ip.bts_addr = *dest;
Harald Welteb77c6972010-05-01 11:28:43 +0200837 nsvc->nsei = nsei;
838 nsvc->nsvci = nsvci;
Harald Welte9f75c352010-04-30 20:26:32 +0200839 nsvc->remote_end_is_sgsn = 1;
840
841 /* Initiate a RESET procedure */
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200842 /* Mark NS-VC locally as blocked and dead */
843 nsvc->state = NSE_S_BLOCKED;
844 /* Send NS-RESET PDU */
Harald Welte90de93e2010-05-03 21:05:24 +0200845 if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200846 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200847 nsei);
848 }
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200849 /* Start Tns-reset */
Harald Welte199d9df2010-05-03 20:55:10 +0200850 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte9f75c352010-04-30 20:26:32 +0200851
852 return nsvc;
853}
Harald Welte1194b582010-05-12 15:55:23 +0000854
855#include <vty/vty.h>
856#include <vty/command.h>
857
858static struct gprs_ns_inst *vty_nsi = NULL;
859
860static struct cmd_node ns_node = {
861 NS_NODE,
862 "%s(ns)#",
863 1,
864};
865
866static int config_write_ns(struct vty *vty)
867{
868 struct gprs_nsvc *nsvc;
869
870 vty_out(vty, "ns%s", VTY_NEWLINE);
871
872 llist_for_each_entry(nsvc, &vty_nsi->gprs_nsvcs, list) {
873 if (!nsvc->persistent)
874 continue;
875 vty_out(vty, " nse %u nsvci %u%s",
876 nsvc->nsei, nsvc->nsvci, VTY_NEWLINE);
877 vty_out(vty, " nse %u remote-role %s%s",
878 nsvc->nsei, nsvc->remote_end_is_sgsn ? "sgsn" : "bss",
879 VTY_NEWLINE);
880 if (nsvc->nsi->ll == GPRS_NS_LL_UDP) {
881 vty_out(vty, " nse %u remote-ip %s%s",
882 nsvc->nsei,
883 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
884 VTY_NEWLINE);
885 vty_out(vty, " nse %u remote-port %u%s",
886 nsvc->nsei, ntohs(nsvc->ip.bts_addr.sin_port),
887 VTY_NEWLINE);
888 }
889 vty_out(vty, "%s", VTY_NEWLINE);
890 }
891
892 return CMD_SUCCESS;
893}
894
895DEFUN(cfg_ns, cfg_ns_cmd,
896 "ns",
897 "Configure the GPRS Network Service")
898{
899 vty->node = NS_NODE;
900 return CMD_SUCCESS;
901}
902
903DEFUN(show_ns, show_ns_cmd, "show ns",
904 SHOW_STR "Display information about the NS protocol")
905{
906 struct gprs_ns_inst *nsi = vty_nsi;
907 struct gprs_nsvc *nsvc;
908
909 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
910 vty_out(vty, "NSEI %5u, NS-VC %5u, Remote: %-4s, %5s %9s",
911 nsvc->nsei, nsvc->nsvci,
912 nsvc->remote_end_is_sgsn ? "SGSN" : "BSS",
913 nsvc->state & NSE_S_ALIVE ? "ALIVE" : "DEAD",
914 nsvc->state & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED");
915 if (nsvc->nsi->ll == GPRS_NS_LL_UDP)
916 vty_out(vty, ", %15s:%u",
917 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
918 ntohs(nsvc->ip.bts_addr.sin_port));
919 vty_out(vty, "%s", VTY_NEWLINE);
920 }
921
922 return CMD_SUCCESS;
923}
924
925
926#define NSE_CMD_STR "NS Entity\n" "NS Entity ID (NSEI)\n"
927
928DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd,
929 "nse <0-65535> nsvci <0-65534>",
930 NSE_CMD_STR
931 "NS Virtual Connection\n"
932 "NS Virtual Connection ID (NSVCI)\n"
933 )
934{
935 uint16_t nsei = atoi(argv[0]);
936 uint16_t nsvci = atoi(argv[1]);
937 struct gprs_nsvc *nsvc;
938
939 nsvc = nsvc_by_nsei(vty_nsi, nsei);
940 if (!nsvc) {
941 nsvc = nsvc_create(vty_nsi, nsvci);
942 nsvc->nsei = nsei;
943 }
944 nsvc->nsvci = nsvci;
945 /* All NSVCs that are explicitly configured by VTY are
946 * marked as persistent so we can write them to the config
947 * file at some later point */
948 nsvc->persistent = 1;
949
950 return CMD_SUCCESS;
951}
952
953DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd,
954 "nse <0-65535> remote-ip A.B.C.D",
955 NSE_CMD_STR
956 "Remote IP Address\n"
957 "Remote IP Address\n")
958{
959 uint16_t nsei = atoi(argv[0]);
960 struct gprs_nsvc *nsvc;
961
962 nsvc = nsvc_by_nsei(vty_nsi, nsei);
963 if (!nsvc) {
964 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
965 return CMD_WARNING;
966 }
967 inet_aton(argv[1], &nsvc->ip.bts_addr.sin_addr);
968
969 return CMD_SUCCESS;
970
971}
972
973DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd,
974 "nse <0-65535> remote-port <0-65535>",
975 NSE_CMD_STR
976 "Remote UDP Port\n"
977 "Remote UDP Port Number\n")
978{
979 uint16_t nsei = atoi(argv[0]);
980 uint16_t port = atoi(argv[1]);
981 struct gprs_nsvc *nsvc;
982
983 nsvc = nsvc_by_nsei(vty_nsi, nsei);
984 if (!nsvc) {
985 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
986 return CMD_WARNING;
987 }
988
989 nsvc->ip.bts_addr.sin_port = htons(port);
990
991 return CMD_SUCCESS;
992}
993
994DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd,
995 "nse <0-65535> remote-role (sgsn|bss)",
996 NSE_CMD_STR
997 "Remote NSE Role\n"
998 "Remote Peer is SGSN\n"
999 "Remote Peer is BSS\n")
1000{
1001 uint16_t nsei = atoi(argv[0]);
1002 struct gprs_nsvc *nsvc;
1003
1004 nsvc = nsvc_by_nsei(vty_nsi, nsei);
1005 if (!nsvc) {
1006 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
1007 return CMD_WARNING;
1008 }
1009
1010 if (!strcmp(argv[1], "sgsn"))
1011 nsvc->remote_end_is_sgsn = 1;
1012 else
1013 nsvc->remote_end_is_sgsn = 0;
1014
1015 return CMD_SUCCESS;
1016}
1017
1018DEFUN(cfg_no_nse, cfg_no_nse_cmd,
1019 "no nse <0-65535>",
1020 "Delete NS Entity\n"
1021 "Delete " NSE_CMD_STR)
1022{
1023 uint16_t nsei = atoi(argv[0]);
1024 struct gprs_nsvc *nsvc;
1025
1026 nsvc = nsvc_by_nsei(vty_nsi, nsei);
1027 if (!nsvc) {
1028 vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE);
1029 return CMD_WARNING;
1030 }
1031
1032 nsvc_delete(nsvc);
1033
1034 return CMD_SUCCESS;
1035}
1036
1037int gprs_ns_vty_init(struct gprs_ns_inst *nsi)
1038{
1039 vty_nsi = nsi;
1040
1041 install_element(VIEW_NODE, &show_ns_cmd);
1042
1043 install_element(CONFIG_NODE, &cfg_ns_cmd);
1044 install_node(&ns_node, config_write_ns);
1045 install_default(NS_NODE);
1046 install_element(NS_NODE, &cfg_nse_nsvci_cmd);
1047 install_element(NS_NODE, &cfg_nse_remoteip_cmd);
1048 install_element(NS_NODE, &cfg_nse_remoteport_cmd);
1049 install_element(NS_NODE, &cfg_nse_remoterole_cmd);
1050 install_element(NS_NODE, &cfg_no_nse_cmd);
1051
1052 return 0;
1053}