blob: 19653f6a650767bdb2c9d36715e7d3b0bfc8931a [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 Welte1389ac72010-05-11 10:15:26 +0200134static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte99e32482010-05-11 06:20:54 +0200135 uint8_t cause)
136{
137 struct ns_signal_data nssd;
138
139 nssd.nsvc = nsvc;
140 nssd.cause = cause;
141
142 dispatch_signal(SS_NS, signal, &nssd);
143}
144
Harald Welte9b455bf2010-03-14 15:45:01 +0800145/* Section 10.3.2, Table 13 */
Harald Welte345223e2010-05-02 09:37:45 +0200146static const struct value_string ns_cause_str[] = {
147 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
148 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
149 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
150 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
151 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
152 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
153 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
154 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte199d9df2010-05-03 20:55:10 +0200155 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte345223e2010-05-02 09:37:45 +0200156 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
157 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
158 { 0, NULL }
Harald Welte9b455bf2010-03-14 15:45:01 +0800159};
160
Harald Welte345223e2010-05-02 09:37:45 +0200161const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800162{
Harald Welte345223e2010-05-02 09:37:45 +0200163 return get_value_string(ns_cause_str, cause);
Harald Welte9b455bf2010-03-14 15:45:01 +0800164}
165
Harald Welte44f1c272010-04-30 19:54:29 +0200166static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltecb991632010-04-26 19:18:54 +0200167
Harald Welte44f1c272010-04-30 19:54:29 +0200168static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800169{
Harald Weltecb991632010-04-26 19:18:54 +0200170 int ret;
171
172 switch (nsvc->nsi->ll) {
173 case GPRS_NS_LL_UDP:
Harald Welte44f1c272010-04-30 19:54:29 +0200174 ret = nsip_sendmsg(nsvc, msg);
Harald Weltecb991632010-04-26 19:18:54 +0200175 break;
176 default:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200177 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
Harald Weltecb991632010-04-26 19:18:54 +0200178 msgb_free(msg);
179 ret = -EIO;
180 break;
181 }
182 return ret;
Harald Welte9b455bf2010-03-14 15:45:01 +0800183}
184
Harald Welteeaa614c2010-05-02 11:26:34 +0200185static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800186{
187 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
188 struct gprs_ns_hdr *nsh;
189
190 if (!msg)
191 return -ENOMEM;
192
193 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
194
195 nsh->pdu_type = pdu_type;
196
Harald Welte44f1c272010-04-30 19:54:29 +0200197 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800198}
199
Harald Welte99e32482010-05-11 06:20:54 +0200200int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte199d9df2010-05-03 20:55:10 +0200201{
202 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
203 struct gprs_ns_hdr *nsh;
Harald Welte199d9df2010-05-03 20:55:10 +0200204 uint16_t nsvci = htons(nsvc->nsvci);
205 uint16_t nsei = htons(nsvc->nsei);
206
207 if (!msg)
208 return -ENOMEM;
209
Harald Welte34caeb32010-05-12 12:01:33 +0000210 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
211 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
212
Harald Welte199d9df2010-05-03 20:55:10 +0200213 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
214 nsh->pdu_type = NS_PDUT_RESET;
215
216 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
217 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
218 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
219
220 return gprs_ns_tx(nsvc, msg);
221
222}
223
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200224int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
225 uint16_t bvci, struct msgb *orig_msg)
226{
227 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
228 struct gprs_ns_hdr *nsh;
229 uint16_t nsvci = htons(nsvc->nsvci);
230
231 bvci = htons(bvci);
232
233 if (!msg)
234 return -ENOMEM;
235
Harald Welte34caeb32010-05-12 12:01:33 +0000236 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
237 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
238
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200239 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
240 nsh->pdu_type = NS_PDUT_STATUS;
241
242 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
243
244 /* Section 9.2.7.1: Static conditions for NS-VCI */
245 if (cause == NS_CAUSE_NSVC_BLOCKED ||
246 cause == NS_CAUSE_NSVC_UNKNOWN)
247 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
248
249 /* Section 9.2.7.2: Static conditions for NS PDU */
250 switch (cause) {
251 case NS_CAUSE_SEM_INCORR_PDU:
252 case NS_CAUSE_PDU_INCOMP_PSTATE:
253 case NS_CAUSE_PROTO_ERR_UNSPEC:
254 case NS_CAUSE_INVAL_ESSENT_IE:
255 case NS_CAUSE_MISSING_ESSENT_IE:
256 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
257 orig_msg->l2h);
258 break;
259 default:
260 break;
261 }
262
263 /* Section 9.2.7.3: Static conditions for BVCI */
264 if (cause == NS_CAUSE_BVCI_UNKNOWN)
265 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
266
267 return gprs_ns_tx(nsvc, msg);
268}
269
Harald Welte99e32482010-05-11 06:20:54 +0200270int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
271{
272 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
273 struct gprs_ns_hdr *nsh;
274 uint16_t nsvci = htons(nsvc->nsvci);
275
276 if (!msg)
277 return -ENOMEM;
278
Harald Welte34caeb32010-05-12 12:01:33 +0000279 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
280 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
281
Harald Welte99e32482010-05-11 06:20:54 +0200282 /* be conservative and mark it as blocked even now! */
283 nsvc->state |= NSE_S_BLOCKED;
284
285 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
286 nsh->pdu_type = NS_PDUT_BLOCK;
287
288 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
289 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
290
291 return gprs_ns_tx(nsvc, msg);
292}
293
294int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
295{
Harald Welte34caeb32010-05-12 12:01:33 +0000296 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
297 nsvc->nsei, nsvc->nsvci);
298
Harald Welte99e32482010-05-11 06:20:54 +0200299 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
300}
301
Harald Weltebca900d2010-05-12 12:11:45 +0000302int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
303{
304 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
305 nsvc->nsei, nsvc->nsvci);
306
307 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
308}
309
310int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
311{
312 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
313 nsvc->nsei, nsvc->nsvci);
314
315 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
316}
317
Harald Welte9b455bf2010-03-14 15:45:01 +0800318#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
319
Harald Welte199d9df2010-05-03 20:55:10 +0200320static const uint8_t timer_mode_tout[_NSVC_TIMER_NR] = {
321 [NSVC_TIMER_TNS_RESET] = 60,
322 [NSVC_TIMER_TNS_ALIVE] = 3,
323 [NSVC_TIMER_TNS_TEST] = 30,
324};
325
326static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
327{
328 nsvc->alive_retries = 0;
329
330 if (bsc_timer_pending(&nsvc->timer))
331 bsc_del_timer(&nsvc->timer);
332
333 nsvc->timer_mode = mode;
334 bsc_schedule_timer(&nsvc->timer, timer_mode_tout[mode], 0);
335}
336
Harald Welte05b320a2010-05-03 20:16:13 +0200337static void gprs_ns_timer_cb(void *data)
Harald Welte9b455bf2010-03-14 15:45:01 +0800338{
339 struct gprs_nsvc *nsvc = data;
340
Harald Welte05b320a2010-05-03 20:16:13 +0200341 switch (nsvc->timer_mode) {
342 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9b455bf2010-03-14 15:45:01 +0800343 /* Tns-alive case: we expired without response ! */
344 nsvc->alive_retries++;
345 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
346 /* mark as dead and blocked */
347 nsvc->state = NSE_S_BLOCKED;
Harald Weltefcc4cc92010-05-12 00:16:57 +0200348 LOGP(DNS, LOGL_NOTICE,
349 "NSEI=%u Tns-alive expired more then "
Harald Welte199d9df2010-05-03 20:55:10 +0200350 "%u times, blocking NS-VC\n", nsvc->nsei,
351 NS_ALIVE_RETRIES);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200352 ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800353 return;
354 }
Harald Weltebca900d2010-05-12 12:11:45 +0000355 /* Tns-test case: send NS-ALIVE PDU */
356 gprs_ns_tx_alive(nsvc);
357 /* start Tns-alive timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200358 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte05b320a2010-05-03 20:16:13 +0200359 break;
360 case NSVC_TIMER_TNS_TEST:
Harald Welte9b455bf2010-03-14 15:45:01 +0800361 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltebca900d2010-05-12 12:11:45 +0000362 gprs_ns_tx_alive(nsvc);
363 /* start Tns-alive timer (transition into faster
364 * alive retransmissions) */
Harald Welte199d9df2010-05-03 20:55:10 +0200365 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
366 break;
367 case NSVC_TIMER_TNS_RESET:
368 /* Chapter 7.3: Re-send the RESET */
Harald Welte90de93e2010-05-03 21:05:24 +0200369 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200370 /* Re-start Tns-reset timer */
Harald Welte199d9df2010-05-03 20:55:10 +0200371 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte05b320a2010-05-03 20:16:13 +0200372 break;
Harald Welte1389ac72010-05-11 10:15:26 +0200373 case _NSVC_TIMER_NR:
374 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800375 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800376}
377
378/* Section 9.2.6 */
Harald Welte5434d7e2010-03-18 00:01:43 +0800379static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9b455bf2010-03-14 15:45:01 +0800380{
381 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
382 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200383 uint16_t nsvci, nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800384
385 if (!msg)
386 return -ENOMEM;
387
Harald Welte5434d7e2010-03-18 00:01:43 +0800388 nsvci = htons(nsvc->nsvci);
389 nsei = htons(nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800390
391 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
392
393 nsh->pdu_type = NS_PDUT_RESET_ACK;
394
Harald Weltefcc4cc92010-05-12 00:16:57 +0200395 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200396 nsvc->nsei, nsvc->nsvci);
Harald Welte5434d7e2010-03-18 00:01:43 +0800397
Harald Welteeaa614c2010-05-02 11:26:34 +0200398 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
399 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800400
Harald Welte44f1c272010-04-30 19:54:29 +0200401 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800402}
403
Harald Welte44f1c272010-04-30 19:54:29 +0200404/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
405int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800406{
Harald Welte44f1c272010-04-30 19:54:29 +0200407 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800408 struct gprs_ns_hdr *nsh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200409 uint16_t bvci = msgb_bvci(msg);
Harald Welte44f1c272010-04-30 19:54:29 +0200410
411 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
412 if (!nsvc) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200413 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
Harald Welte239cf052010-05-03 18:54:12 +0200414 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte44f1c272010-04-30 19:54:29 +0200415 return -EINVAL;
416 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800417
Harald Welte199d9df2010-05-03 20:55:10 +0200418 if (!(nsvc->state & NSE_S_ALIVE)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200419 LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200420 nsvc->nsei);
421 return -EBUSY;
422 }
423 if (nsvc->state & NSE_S_BLOCKED) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200424 LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200425 nsvc->nsei);
426 return -EBUSY;
427 }
428
Harald Welte9b455bf2010-03-14 15:45:01 +0800429 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
430 if (!nsh) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200431 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800432 return -EIO;
433 }
434
435 nsh->pdu_type = NS_PDUT_UNITDATA;
436 /* spare octet in data[0] */
437 nsh->data[1] = bvci >> 8;
438 nsh->data[2] = bvci & 0xff;
439
Harald Welte44f1c272010-04-30 19:54:29 +0200440 return gprs_ns_tx(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800441}
442
443/* Section 9.2.10: receive side */
Harald Welte44f1c272010-04-30 19:54:29 +0200444static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800445{
446 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welteeaa614c2010-05-02 11:26:34 +0200447 uint16_t bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800448
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200449 if (nsvc->state & NSE_S_BLOCKED)
450 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
451 0, msg);
452
Harald Welte9b455bf2010-03-14 15:45:01 +0800453 /* spare octet in data[0] */
454 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200455 msgb_bssgph(msg) = &nsh->data[3];
Harald Weltee6afd602010-05-02 11:19:37 +0200456 msgb_bvci(msg) = bvci;
Harald Welte9b455bf2010-03-14 15:45:01 +0800457
458 /* call upper layer (BSSGP) */
Harald Weltecb991632010-04-26 19:18:54 +0200459 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800460}
461
462/* Section 9.2.7 */
Harald Welte44f1c272010-04-30 19:54:29 +0200463static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800464{
Harald Weltecb991632010-04-26 19:18:54 +0200465 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9b455bf2010-03-14 15:45:01 +0800466 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200467 uint8_t cause;
Harald Welte9b455bf2010-03-14 15:45:01 +0800468 int rc;
469
Harald Weltefcc4cc92010-05-12 00:16:57 +0200470 LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800471
472 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
473
474 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltefcc4cc92010-05-12 00:16:57 +0200475 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800476 return -EINVAL;
477 }
478
479 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Weltefcc4cc92010-05-12 00:16:57 +0200480 LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800481
482 return 0;
483}
484
485/* Section 7.3 */
Harald Welte44f1c272010-04-30 19:54:29 +0200486static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800487{
488 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;
491 uint16_t *nsvci, *nsei;
Harald Welte9b455bf2010-03-14 15:45:01 +0800492 int rc;
493
Harald Welte9b455bf2010-03-14 15:45:01 +0800494 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
495
496 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
497 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
498 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200499 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200500 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800501 return -EINVAL;
502 }
503
Harald Welteeaa614c2010-05-02 11:26:34 +0200504 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
505 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
506 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9b455bf2010-03-14 15:45:01 +0800507
Harald Weltefcc4cc92010-05-12 00:16:57 +0200508 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Welte239cf052010-05-03 18:54:12 +0200509 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
510
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200511 /* Mark NS-VC as blocked and alive */
Harald Welte5434d7e2010-03-18 00:01:43 +0800512 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welted7c02ad2010-05-11 18:18:31 +0200513
Harald Welte5434d7e2010-03-18 00:01:43 +0800514 nsvc->nsei = ntohs(*nsei);
515 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800516
Harald Welte9b455bf2010-03-14 15:45:01 +0800517 /* start the test procedure */
Harald Welte199d9df2010-05-03 20:55:10 +0200518 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9b455bf2010-03-14 15:45:01 +0800519
Harald Welte99e32482010-05-11 06:20:54 +0200520 /* inform interested parties about the fact that this NSVC
521 * has received RESET */
Harald Welte1389ac72010-05-11 10:15:26 +0200522 ns_dispatch_signal(nsvc, S_NS_RESET, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200523
Harald Welte5434d7e2010-03-18 00:01:43 +0800524 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800525}
526
Harald Welte99e32482010-05-11 06:20:54 +0200527static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
528{
529 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
530 struct tlv_parsed tp;
531 uint8_t *cause;
532 int rc;
533
Harald Weltefcc4cc92010-05-12 00:16:57 +0200534 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte99e32482010-05-11 06:20:54 +0200535
536 nsvc->state |= NSE_S_BLOCKED;
537
538 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
539
540 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
541 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte99e32482010-05-11 06:20:54 +0200542 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200543 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte99e32482010-05-11 06:20:54 +0200544 return -EINVAL;
545 }
546
547 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
548 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
549
Harald Welte1389ac72010-05-11 10:15:26 +0200550 ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause);
Harald Welte99e32482010-05-11 06:20:54 +0200551
552 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
553}
554
Harald Welte9b455bf2010-03-14 15:45:01 +0800555/* main entry point, here incoming NS frames enter */
Harald Weltecb991632010-04-26 19:18:54 +0200556int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
557 struct sockaddr_in *saddr)
Harald Welte9b455bf2010-03-14 15:45:01 +0800558{
559 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltecb991632010-04-26 19:18:54 +0200560 struct gprs_nsvc *nsvc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800561 int rc = 0;
562
Harald Weltecb991632010-04-26 19:18:54 +0200563 /* look up the NSVC based on source address */
564 nsvc = nsvc_by_rem_addr(nsi, saddr);
565 if (!nsvc) {
Harald Weltee5117da2010-05-11 18:30:37 +0200566 struct tlv_parsed tp;
567 uint16_t nsei;
Harald Weltecb991632010-04-26 19:18:54 +0200568 /* Only the RESET procedure creates a new NSVC */
Harald Welte239cf052010-05-03 18:54:12 +0200569 if (nsh->pdu_type != NS_PDUT_RESET) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200570 LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x "
Harald Welteef1226e2010-05-11 18:38:36 +0200571 "from %s:%u for non-existing NS-VC\n",
572 nsh->pdu_type, inet_ntoa(saddr->sin_addr),
573 ntohs(saddr->sin_port));
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200574 /* FIXME: send STATUS (but we have no NSVC!) */
Harald Welte90de93e2010-05-03 21:05:24 +0200575 //gprs_ns_tx_reset(nsvc, NS_CAUSE_NSVC_UNKNOWN);
Harald Weltecb991632010-04-26 19:18:54 +0200576 return -EIO;
Harald Welte239cf052010-05-03 18:54:12 +0200577 }
Harald Weltee5117da2010-05-11 18:30:37 +0200578 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
579 msgb_l2len(msg), 0, 0);
580 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
581 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
582 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Weltee5117da2010-05-11 18:30:37 +0200583 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200584 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
585 msg);
Harald Weltee5117da2010-05-11 18:30:37 +0200586 return -EINVAL;
587 }
588 nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI));
589 /* Check if we already know this NSEI, the remote end might
590 * simply have changed addresses, or it is a SGSN */
591 nsvc = nsvc_by_nsei(nsi, nsei);
592 if (!nsvc) {
593 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
594 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
595 nsvc = nsvc_create(nsi, 0xffff);
Harald Weltee5117da2010-05-11 18:30:37 +0200596 }
Harald Welte7c209eb2010-05-11 18:40:45 +0200597 /* Update the remote peer IP address/port */
598 nsvc->ip.bts_addr = *saddr;
Harald Welte239cf052010-05-03 18:54:12 +0200599 } else
600 msgb_nsei(msg) = nsvc->nsei;
Harald Welte5434d7e2010-03-18 00:01:43 +0800601
Harald Welte9b455bf2010-03-14 15:45:01 +0800602 switch (nsh->pdu_type) {
603 case NS_PDUT_ALIVE:
604 /* remote end inquires whether we're still alive,
605 * we need to respond with ALIVE_ACK */
Harald Weltebca900d2010-05-12 12:11:45 +0000606 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9b455bf2010-03-14 15:45:01 +0800607 break;
608 case NS_PDUT_ALIVE_ACK:
609 /* stop Tns-alive */
Harald Welte05b320a2010-05-03 20:16:13 +0200610 bsc_del_timer(&nsvc->timer);
Harald Welte9b455bf2010-03-14 15:45:01 +0800611 /* start Tns-test */
Harald Welte199d9df2010-05-03 20:55:10 +0200612 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte24b31312010-05-03 21:11:22 +0200613 if (nsvc->remote_end_is_sgsn) {
614 /* FIXME: this should be one level higher */
615 if (nsvc->state & NSE_S_BLOCKED)
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200616 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte24b31312010-05-03 21:11:22 +0200617 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800618 break;
619 case NS_PDUT_UNITDATA:
620 /* actual user data */
Harald Welte44f1c272010-04-30 19:54:29 +0200621 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800622 break;
623 case NS_PDUT_STATUS:
Harald Welte44f1c272010-04-30 19:54:29 +0200624 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800625 break;
626 case NS_PDUT_RESET:
Harald Welte44f1c272010-04-30 19:54:29 +0200627 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800628 break;
629 case NS_PDUT_RESET_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200630 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200631 /* mark NS-VC as blocked + active */
632 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200633 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte199d9df2010-05-03 20:55:10 +0200634 if (nsvc->remote_end_is_sgsn) {
635 /* stop RESET timer */
636 bsc_del_timer(&nsvc->timer);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200637 /* Initiate TEST proc.: Send ALIVE and start timer */
Harald Welte90de93e2010-05-03 21:05:24 +0200638 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
639 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte199d9df2010-05-03 20:55:10 +0200640 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800641 break;
642 case NS_PDUT_UNBLOCK:
643 /* Section 7.2: unblocking procedure */
Harald Weltefcc4cc92010-05-12 00:16:57 +0200644 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800645 nsvc->state &= ~NSE_S_BLOCKED;
Harald Welte99e32482010-05-11 06:20:54 +0200646 ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0);
Harald Welte5434d7e2010-03-18 00:01:43 +0800647 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9b455bf2010-03-14 15:45:01 +0800648 break;
649 case NS_PDUT_UNBLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200650 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200651 /* mark NS-VC as unblocked + active */
652 nsvc->state = NSE_S_ALIVE;
Harald Weltecb991632010-04-26 19:18:54 +0200653 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte9b455bf2010-03-14 15:45:01 +0800654 break;
655 case NS_PDUT_BLOCK:
Harald Welte99e32482010-05-11 06:20:54 +0200656 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800657 break;
658 case NS_PDUT_BLOCK_ACK:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200659 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltecb991632010-04-26 19:18:54 +0200660 /* mark remote NS-VC as blocked + active */
661 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9b455bf2010-03-14 15:45:01 +0800662 break;
663 default:
Harald Weltefcc4cc92010-05-12 00:16:57 +0200664 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Welte239cf052010-05-03 18:54:12 +0200665 nsvc->nsei, nsh->pdu_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800666 rc = -EINVAL;
667 break;
668 }
669 return rc;
670}
671
Harald Weltecb991632010-04-26 19:18:54 +0200672struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
673{
674 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
675
676 nsi->cb = cb;
677 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
678
Harald Welte9f75c352010-04-30 20:26:32 +0200679 return nsi;
Harald Weltecb991632010-04-26 19:18:54 +0200680}
681
682void gprs_ns_destroy(struct gprs_ns_inst *nsi)
683{
684 /* FIXME: clear all timers */
685
686 /* recursively free the NSI and all its NSVCs */
687 talloc_free(nsi);
688}
689
690
691/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
692 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
693
694/* Read a single NS-over-IP message */
695static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
696 struct sockaddr_in *saddr)
697{
698 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
699 int ret = 0;
700 socklen_t saddr_len = sizeof(*saddr);
701
702 if (!msg) {
703 *error = -ENOMEM;
704 return NULL;
705 }
706
707 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
708 (struct sockaddr *)saddr, &saddr_len);
709 if (ret < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200710 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Welte239cf052010-05-03 18:54:12 +0200711 strerror(errno));
Harald Weltecb991632010-04-26 19:18:54 +0200712 msgb_free(msg);
713 *error = ret;
714 return NULL;
715 } else if (ret == 0) {
716 msgb_free(msg);
717 *error = ret;
718 return NULL;
719 }
720
721 msg->l2h = msg->data;
722 msgb_put(msg, ret);
723
724 return msg;
725}
726
727static int handle_nsip_read(struct bsc_fd *bfd)
728{
729 int error;
730 struct sockaddr_in saddr;
731 struct gprs_ns_inst *nsi = bfd->data;
732 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
733
734 if (!msg)
735 return error;
736
737 return gprs_ns_rcvmsg(nsi, msg, &saddr);
738}
739
740static int handle_nsip_write(struct bsc_fd *bfd)
741{
742 /* FIXME: actually send the data here instead of nsip_sendmsg() */
743 return -EIO;
744}
745
Harald Welte44f1c272010-04-30 19:54:29 +0200746int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltecb991632010-04-26 19:18:54 +0200747{
748 int rc;
749 struct gprs_ns_inst *nsi = nsvc->nsi;
750 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
751
752 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
753 (struct sockaddr *)daddr, sizeof(*daddr));
754
755 talloc_free(msg);
756
757 return rc;
758}
759
760/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
761static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
762{
763 int rc = 0;
764
765 if (what & BSC_FD_READ)
766 rc = handle_nsip_read(bfd);
767 if (what & BSC_FD_WRITE)
768 rc = handle_nsip_write(bfd);
769
770 return rc;
771}
772
773
774/* FIXME: this is currently in input/ipaccess.c */
Harald Welteeaa614c2010-05-02 11:26:34 +0200775extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltecb991632010-04-26 19:18:54 +0200776 int (*cb)(struct bsc_fd *fd, unsigned int what));
777
778/* Listen for incoming GPRS packets */
779int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
780{
781 int ret;
782
783 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
784 if (ret < 0)
785 return ret;
786
787 nsi->ll = GPRS_NS_LL_UDP;
788 nsi->nsip.fd.data = nsi;
789
790 return ret;
791}
Harald Welte9f75c352010-04-30 20:26:32 +0200792
793/* Establish a connection (from the BSS) to the SGSN */
794struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welteb77c6972010-05-01 11:28:43 +0200795 struct sockaddr_in *dest, uint16_t nsei,
796 uint16_t nsvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200797{
798 struct gprs_nsvc *nsvc;
799
800 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Weltecad83012010-05-12 11:56:39 +0000801 if (!nsvc)
Harald Welte9f75c352010-04-30 20:26:32 +0200802 nsvc = nsvc_create(nsi, nsvci);
Harald Weltecad83012010-05-12 11:56:39 +0000803 nsvc->ip.bts_addr = *dest;
Harald Welteb77c6972010-05-01 11:28:43 +0200804 nsvc->nsei = nsei;
805 nsvc->nsvci = nsvci;
Harald Welte9f75c352010-04-30 20:26:32 +0200806 nsvc->remote_end_is_sgsn = 1;
807
808 /* Initiate a RESET procedure */
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200809 /* Mark NS-VC locally as blocked and dead */
810 nsvc->state = NSE_S_BLOCKED;
811 /* Send NS-RESET PDU */
Harald Welte90de93e2010-05-03 21:05:24 +0200812 if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200813 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
Harald Welte199d9df2010-05-03 20:55:10 +0200814 nsei);
815 }
Harald Welte9bdb3ba2010-05-12 11:48:44 +0200816 /* Start Tns-reset */
Harald Welte199d9df2010-05-03 20:55:10 +0200817 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte9f75c352010-04-30 20:26:32 +0200818
819 return nsvc;
820}