blob: 39ca8101ff223cbfe1a9ebdeb9b052149bc5c24c [file] [log] [blame]
Harald Welte24a655f2010-04-30 19:54:29 +02001/* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg);
Harald Welte9ba50052010-03-14 15:45:01 +08002 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
Harald Weltef030b212010-04-26 19:18:54 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24/* Some introduction into NS: NS is used typically on top of frame relay,
25 * but in the ip.access world it is encapsulated in UDP packets. It serves
26 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
27 * do much, apart from providing congestion notification and status indication.
28 *
29 * Terms:
30 * NS Network Service
31 * NSVC NS Virtual Connection
32 * NSEI NS Entity Identifier
33 * NSVL NS Virtual Link
34 * NSVLI NS Virtual Link Identifier
35 * BVC BSSGP Virtual Connection
36 * BVCI BSSGP Virtual Connection Identifier
37 * NSVCG NS Virtual Connection Goup
38 * Blocked NS-VC cannot be used for user traffic
39 * Alive Ability of a NS-VC to provide communication
40 *
41 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
42 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
43 * NS then has to firgure out which NSVC's are responsible for this BVCI.
44 * Those mappings are administratively configured.
45 */
46
47#include <stdlib.h>
48#include <unistd.h>
49#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020050#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080051
52#include <arpa/inet.h>
53
54#include <openbsc/gsm_data.h>
55#include <osmocore/msgb.h>
56#include <osmocore/tlv.h>
57#include <osmocore/talloc.h>
Harald Weltef030b212010-04-26 19:18:54 +020058#include <osmocore/select.h>
Harald Welte9ba50052010-03-14 15:45:01 +080059#include <openbsc/debug.h>
60#include <openbsc/gprs_ns.h>
61#include <openbsc/gprs_bssgp.h>
62
63#define NS_ALLOC_SIZE 1024
64
65static const struct tlv_definition ns_att_tlvdef = {
66 .def = {
67 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
68 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
69 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
70 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
71 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
72 },
73};
74
Harald Weltef030b212010-04-26 19:18:54 +020075/* Lookup struct gprs_nsvc based on NSVCI */
76static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020077 uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +020078{
79 struct gprs_nsvc *nsvc;
80 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
81 if (nsvc->nsvci == nsvci)
82 return nsvc;
83 }
84 return NULL;
85}
86
Harald Welte24a655f2010-04-30 19:54:29 +020087/* Lookup struct gprs_nsvc based on NSVCI */
88static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020089 uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +020090{
91 struct gprs_nsvc *nsvc;
92 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
93 if (nsvc->nsei == nsei)
94 return nsvc;
95 }
96 return NULL;
97}
98
99
Harald Weltef030b212010-04-26 19:18:54 +0200100/* Lookup struct gprs_nsvc based on remote peer socket addr */
101static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
102 struct sockaddr_in *sin)
103{
104 struct gprs_nsvc *nsvc;
105 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
106 if (!memcmp(&nsvc->ip.bts_addr, sin, sizeof(*sin)))
107 return nsvc;
108 }
109 return NULL;
110}
111
Harald Welte69a4cf22010-05-03 20:55:10 +0200112static void gprs_ns_timer_cb(void *data);
113
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200114static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200115{
116 struct gprs_nsvc *nsvc;
117
118 nsvc = talloc_zero(nsi, struct gprs_nsvc);
119 nsvc->nsvci = nsvci;
120 /* before RESET procedure: BLOCKED and DEAD */
121 nsvc->state = NSE_S_BLOCKED;
122 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200123 nsvc->timer.cb = gprs_ns_timer_cb;
124 nsvc->timer.data = nsvc;
125
Harald Weltef030b212010-04-26 19:18:54 +0200126 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
127
128 return nsvc;
129}
Harald Welte9ba50052010-03-14 15:45:01 +0800130
131/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200132static const struct value_string ns_cause_str[] = {
133 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
134 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
135 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
136 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
137 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
138 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
139 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
140 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200141 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200142 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
143 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
144 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800145};
146
Harald Welte2577d412010-05-02 09:37:45 +0200147const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800148{
Harald Welte2577d412010-05-02 09:37:45 +0200149 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800150}
151
Harald Welte24a655f2010-04-30 19:54:29 +0200152static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200153
Harald Welte24a655f2010-04-30 19:54:29 +0200154static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800155{
Harald Weltef030b212010-04-26 19:18:54 +0200156 int ret;
157
158 switch (nsvc->nsi->ll) {
159 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200160 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200161 break;
162 default:
163 LOGP(DGPRS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
164 msgb_free(msg);
165 ret = -EIO;
166 break;
167 }
168 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800169}
170
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200171static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800172{
173 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
174 struct gprs_ns_hdr *nsh;
175
176 if (!msg)
177 return -ENOMEM;
178
179 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
180
181 nsh->pdu_type = pdu_type;
182
Harald Welte24a655f2010-04-30 19:54:29 +0200183 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800184}
185
Harald Welte69a4cf22010-05-03 20:55:10 +0200186static int gprs_ns_tx_reset(struct gprs_nsvc *nsvc)
187{
188 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
189 struct gprs_ns_hdr *nsh;
190 uint8_t cause = NS_CAUSE_OM_INTERVENTION;
191 uint16_t nsvci = htons(nsvc->nsvci);
192 uint16_t nsei = htons(nsvc->nsei);
193
194 if (!msg)
195 return -ENOMEM;
196
197 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
198 nsh->pdu_type = NS_PDUT_RESET;
199
200 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
201 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
202 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
203
204 return gprs_ns_tx(nsvc, msg);
205
206}
207
Harald Welte9ba50052010-03-14 15:45:01 +0800208#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
209
Harald Welte69a4cf22010-05-03 20:55:10 +0200210static const uint8_t timer_mode_tout[_NSVC_TIMER_NR] = {
211 [NSVC_TIMER_TNS_RESET] = 60,
212 [NSVC_TIMER_TNS_ALIVE] = 3,
213 [NSVC_TIMER_TNS_TEST] = 30,
214};
215
216static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
217{
218 nsvc->alive_retries = 0;
219
220 if (bsc_timer_pending(&nsvc->timer))
221 bsc_del_timer(&nsvc->timer);
222
223 nsvc->timer_mode = mode;
224 bsc_schedule_timer(&nsvc->timer, timer_mode_tout[mode], 0);
225}
226
Harald Welte80405452010-05-03 20:16:13 +0200227static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800228{
229 struct gprs_nsvc *nsvc = data;
230
Harald Welte80405452010-05-03 20:16:13 +0200231 switch (nsvc->timer_mode) {
232 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800233 /* Tns-alive case: we expired without response ! */
234 nsvc->alive_retries++;
235 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
236 /* mark as dead and blocked */
237 nsvc->state = NSE_S_BLOCKED;
Harald Welte69a4cf22010-05-03 20:55:10 +0200238 DEBUGP(DGPRS, "NSEI=%u Tns-alive expired more then "
239 "%u times, blocking NS-VC\n", nsvc->nsei,
240 NS_ALIVE_RETRIES);
Harald Welte9ba50052010-03-14 15:45:01 +0800241 /* FIXME: inform higher layers */
242 return;
243 }
Harald Welte69a4cf22010-05-03 20:55:10 +0200244 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200245 break;
246 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800247 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltec5478482010-03-18 00:01:43 +0800248 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800249 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200250 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
251 break;
252 case NSVC_TIMER_TNS_RESET:
253 /* Chapter 7.3: Re-send the RESET */
254 gprs_ns_tx_reset(nsvc);
255 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200256 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800257 }
Harald Welte9ba50052010-03-14 15:45:01 +0800258}
259
260/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800261static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800262{
263 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
264 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200265 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800266
267 if (!msg)
268 return -ENOMEM;
269
Harald Weltec5478482010-03-18 00:01:43 +0800270 nsvci = htons(nsvc->nsvci);
271 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800272
273 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
274
275 nsh->pdu_type = NS_PDUT_RESET_ACK;
276
Harald Weltebbc9fac2010-05-03 18:54:12 +0200277 DEBUGP(DGPRS, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
278 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800279
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200280 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
281 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800282
Harald Welte24a655f2010-04-30 19:54:29 +0200283 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800284}
285
Harald Welte24a655f2010-04-30 19:54:29 +0200286/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
287int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800288{
Harald Welte24a655f2010-04-30 19:54:29 +0200289 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800290 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200291 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200292
293 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
294 if (!nsvc) {
Harald Weltebbc9fac2010-05-03 18:54:12 +0200295 LOGP(DGPRS, LOGL_ERROR, "Unable to resolve NSEI %u "
296 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200297 return -EINVAL;
298 }
Harald Welte9ba50052010-03-14 15:45:01 +0800299
Harald Welte69a4cf22010-05-03 20:55:10 +0200300 if (!(nsvc->state & NSE_S_ALIVE)) {
301 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n",
302 nsvc->nsei);
303 return -EBUSY;
304 }
305 if (nsvc->state & NSE_S_BLOCKED) {
306 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n",
307 nsvc->nsei);
308 return -EBUSY;
309 }
310
Harald Welte9ba50052010-03-14 15:45:01 +0800311 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
312 if (!nsh) {
Harald Weltebbc9fac2010-05-03 18:54:12 +0200313 LOGP(DGPRS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800314 return -EIO;
315 }
316
317 nsh->pdu_type = NS_PDUT_UNITDATA;
318 /* spare octet in data[0] */
319 nsh->data[1] = bvci >> 8;
320 nsh->data[2] = bvci & 0xff;
321
Harald Welte24a655f2010-04-30 19:54:29 +0200322 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800323}
324
325/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200326static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800327{
328 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200329 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800330
331 /* spare octet in data[0] */
332 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200333 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200334 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800335
336 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200337 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800338}
339
340/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200341static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800342{
Harald Weltef030b212010-04-26 19:18:54 +0200343 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800344 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200345 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800346 int rc;
347
Harald Weltebbc9fac2010-05-03 18:54:12 +0200348 DEBUGP(DGPRS, "NSEI=%u NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800349
350 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
351
352 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
353 DEBUGPC(DGPRS, "missing cause IE\n");
354 return -EINVAL;
355 }
356
357 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
358 DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause));
359
360 return 0;
361}
362
363/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200364static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800365{
366 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800367 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200368 uint8_t *cause;
369 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800370 int rc;
371
Harald Welte9ba50052010-03-14 15:45:01 +0800372 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
373
374 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
375 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
376 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
377 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200378 LOGP(DGPRS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800379 return -EINVAL;
380 }
381
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200382 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
383 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
384 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800385
Harald Weltebbc9fac2010-05-03 18:54:12 +0200386 DEBUGP(DGPRS, "NSEI=%u NS RESET (NSVCI=%u, cause=%s)\n",
387 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
388
Harald Weltec5478482010-03-18 00:01:43 +0800389 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
390 nsvc->nsei = ntohs(*nsei);
391 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800392
Harald Welte9ba50052010-03-14 15:45:01 +0800393 /* mark the NS-VC as blocked and alive */
Harald Welte9ba50052010-03-14 15:45:01 +0800394 /* start the test procedure */
Harald Welte69a4cf22010-05-03 20:55:10 +0200395 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800396
Harald Weltec5478482010-03-18 00:01:43 +0800397 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800398}
399
400/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200401int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
402 struct sockaddr_in *saddr)
Harald Welte9ba50052010-03-14 15:45:01 +0800403{
404 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200405 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800406 int rc = 0;
407
Harald Weltef030b212010-04-26 19:18:54 +0200408 /* look up the NSVC based on source address */
409 nsvc = nsvc_by_rem_addr(nsi, saddr);
410 if (!nsvc) {
411 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200412 if (nsh->pdu_type != NS_PDUT_RESET) {
413 LOGP(DGPRS, LOGL_INFO, "Ignoring NS PDU type 0x%0x "
414 "from %s for non-existing NS-VC\n",
415 nsh->pdu_type, inet_ntoa(saddr->sin_addr));
Harald Weltef030b212010-04-26 19:18:54 +0200416 return -EIO;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200417 }
418 LOGP(DGPRS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
419 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Weltef030b212010-04-26 19:18:54 +0200420 nsvc = nsvc_create(nsi, 0xffff);
421 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200422 } else
423 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800424
Harald Welte9ba50052010-03-14 15:45:01 +0800425 switch (nsh->pdu_type) {
426 case NS_PDUT_ALIVE:
427 /* remote end inquires whether we're still alive,
428 * we need to respond with ALIVE_ACK */
Harald Weltec5478482010-03-18 00:01:43 +0800429 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800430 break;
431 case NS_PDUT_ALIVE_ACK:
432 /* stop Tns-alive */
Harald Welte80405452010-05-03 20:16:13 +0200433 bsc_del_timer(&nsvc->timer);
Harald Welte9ba50052010-03-14 15:45:01 +0800434 /* start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200435 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +0800436 break;
437 case NS_PDUT_UNITDATA:
438 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200439 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800440 break;
441 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200442 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800443 break;
444 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200445 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800446 break;
447 case NS_PDUT_RESET_ACK:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200448 DEBUGP(DGPRS, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200449 /* mark remote NS-VC as blocked + active */
450 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte69a4cf22010-05-03 20:55:10 +0200451 if (nsvc->remote_end_is_sgsn) {
452 /* stop RESET timer */
453 bsc_del_timer(&nsvc->timer);
454 }
Harald Welte9ba50052010-03-14 15:45:01 +0800455 break;
456 case NS_PDUT_UNBLOCK:
457 /* Section 7.2: unblocking procedure */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200458 DEBUGP(DGPRS, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800459 nsvc->state &= ~NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800460 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800461 break;
462 case NS_PDUT_UNBLOCK_ACK:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200463 DEBUGP(DGPRS, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200464 /* mark remote NS-VC as unblocked + active */
465 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800466 break;
467 case NS_PDUT_BLOCK:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200468 DEBUGP(DGPRS, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800469 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800470 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800471 break;
472 case NS_PDUT_BLOCK_ACK:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200473 DEBUGP(DGPRS, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200474 /* mark remote NS-VC as blocked + active */
475 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800476 break;
477 default:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200478 DEBUGP(DGPRS, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
479 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800480 rc = -EINVAL;
481 break;
482 }
483 return rc;
484}
485
Harald Weltef030b212010-04-26 19:18:54 +0200486struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
487{
488 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
489
490 nsi->cb = cb;
491 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
492
Harald Welte3771d092010-04-30 20:26:32 +0200493 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200494}
495
496void gprs_ns_destroy(struct gprs_ns_inst *nsi)
497{
498 /* FIXME: clear all timers */
499
500 /* recursively free the NSI and all its NSVCs */
501 talloc_free(nsi);
502}
503
504
505/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
506 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
507
508/* Read a single NS-over-IP message */
509static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
510 struct sockaddr_in *saddr)
511{
512 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
513 int ret = 0;
514 socklen_t saddr_len = sizeof(*saddr);
515
516 if (!msg) {
517 *error = -ENOMEM;
518 return NULL;
519 }
520
521 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
522 (struct sockaddr *)saddr, &saddr_len);
523 if (ret < 0) {
Harald Weltebbc9fac2010-05-03 18:54:12 +0200524 LOGP(DGPRS, LOGL_ERROR, "recv error %s during NSIP recv\n",
525 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200526 msgb_free(msg);
527 *error = ret;
528 return NULL;
529 } else if (ret == 0) {
530 msgb_free(msg);
531 *error = ret;
532 return NULL;
533 }
534
535 msg->l2h = msg->data;
536 msgb_put(msg, ret);
537
538 return msg;
539}
540
541static int handle_nsip_read(struct bsc_fd *bfd)
542{
543 int error;
544 struct sockaddr_in saddr;
545 struct gprs_ns_inst *nsi = bfd->data;
546 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
547
548 if (!msg)
549 return error;
550
551 return gprs_ns_rcvmsg(nsi, msg, &saddr);
552}
553
554static int handle_nsip_write(struct bsc_fd *bfd)
555{
556 /* FIXME: actually send the data here instead of nsip_sendmsg() */
557 return -EIO;
558}
559
Harald Welte24a655f2010-04-30 19:54:29 +0200560int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200561{
562 int rc;
563 struct gprs_ns_inst *nsi = nsvc->nsi;
564 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
565
566 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
567 (struct sockaddr *)daddr, sizeof(*daddr));
568
569 talloc_free(msg);
570
571 return rc;
572}
573
574/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
575static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
576{
577 int rc = 0;
578
579 if (what & BSC_FD_READ)
580 rc = handle_nsip_read(bfd);
581 if (what & BSC_FD_WRITE)
582 rc = handle_nsip_write(bfd);
583
584 return rc;
585}
586
587
588/* FIXME: this is currently in input/ipaccess.c */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200589extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltef030b212010-04-26 19:18:54 +0200590 int (*cb)(struct bsc_fd *fd, unsigned int what));
591
592/* Listen for incoming GPRS packets */
593int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
594{
595 int ret;
596
597 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
598 if (ret < 0)
599 return ret;
600
601 nsi->ll = GPRS_NS_LL_UDP;
602 nsi->nsip.fd.data = nsi;
603
604 return ret;
605}
Harald Welte3771d092010-04-30 20:26:32 +0200606
607/* Establish a connection (from the BSS) to the SGSN */
608struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200609 struct sockaddr_in *dest, uint16_t nsei,
610 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200611{
612 struct gprs_nsvc *nsvc;
613
614 nsvc = nsvc_by_rem_addr(nsi, dest);
615 if (!nsvc) {
616 nsvc = nsvc_create(nsi, nsvci);
617 nsvc->ip.bts_addr = *dest;
618 }
Harald Welte1203de32010-05-01 11:28:43 +0200619 nsvc->nsei = nsei;
620 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200621 nsvc->remote_end_is_sgsn = 1;
622
623 /* Initiate a RESET procedure */
Harald Welte69a4cf22010-05-03 20:55:10 +0200624 if (gprs_ns_tx_reset(nsvc) < 0) {
625 LOGP(DGPRS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
626 nsei);
627 }
628 /* run a timer and re-transmit the reset request? */
629 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte3771d092010-04-30 20:26:32 +0200630
631 return nsvc;
632}