blob: 9c198ad9a0e0eec60c6e6e3770b60e1eb2671339 [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 Welte8f9a3ee2010-05-02 11:26:34 +0200112static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200113{
114 struct gprs_nsvc *nsvc;
115
116 nsvc = talloc_zero(nsi, struct gprs_nsvc);
117 nsvc->nsvci = nsvci;
118 /* before RESET procedure: BLOCKED and DEAD */
119 nsvc->state = NSE_S_BLOCKED;
120 nsvc->nsi = nsi;
121 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
122
123 return nsvc;
124}
Harald Welte9ba50052010-03-14 15:45:01 +0800125
126/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200127static const struct value_string ns_cause_str[] = {
128 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
129 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
130 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
131 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
132 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
133 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
134 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
135 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
136 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error }, unspecified" },
137 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
138 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
139 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800140};
141
Harald Welte2577d412010-05-02 09:37:45 +0200142const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800143{
Harald Welte2577d412010-05-02 09:37:45 +0200144 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800145}
146
Harald Welte24a655f2010-04-30 19:54:29 +0200147static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200148
Harald Welte24a655f2010-04-30 19:54:29 +0200149static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800150{
Harald Weltef030b212010-04-26 19:18:54 +0200151 int ret;
152
153 switch (nsvc->nsi->ll) {
154 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200155 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200156 break;
157 default:
158 LOGP(DGPRS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll);
159 msgb_free(msg);
160 ret = -EIO;
161 break;
162 }
163 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800164}
165
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200166static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800167{
168 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
169 struct gprs_ns_hdr *nsh;
170
171 if (!msg)
172 return -ENOMEM;
173
174 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
175
176 nsh->pdu_type = pdu_type;
177
Harald Welte24a655f2010-04-30 19:54:29 +0200178 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800179}
180
181#define NS_TIMER_ALIVE 3, 0 /* after 3 seconds without response, we retry */
182#define NS_TIMER_TEST 30, 0 /* every 10 seconds we check if the BTS is still alive */
183#define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */
184
Harald Welte80405452010-05-03 20:16:13 +0200185static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800186{
187 struct gprs_nsvc *nsvc = data;
188
Harald Welte80405452010-05-03 20:16:13 +0200189 switch (nsvc->timer_mode) {
190 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800191 /* Tns-alive case: we expired without response ! */
192 nsvc->alive_retries++;
193 if (nsvc->alive_retries > NS_ALIVE_RETRIES) {
194 /* mark as dead and blocked */
195 nsvc->state = NSE_S_BLOCKED;
196 DEBUGP(DGPRS, "Tns-alive more then %u retries, "
197 " blocking NS-VC\n", NS_ALIVE_RETRIES);
198 /* FIXME: inform higher layers */
199 return;
200 }
Harald Welte80405452010-05-03 20:16:13 +0200201 break;
202 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800203 /* Tns-test case: send NS-ALIVE PDU */
Harald Weltec5478482010-03-18 00:01:43 +0800204 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800205 /* start Tns-alive timer */
Harald Welte80405452010-05-03 20:16:13 +0200206 nsvc->timer_mode = NSVC_TIMER_TNS_ALIVE;
207 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800208 }
Harald Welte80405452010-05-03 20:16:13 +0200209 bsc_schedule_timer(&nsvc->timer, NS_TIMER_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800210}
211
212/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800213static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800214{
215 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS");
216 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200217 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800218
219 if (!msg)
220 return -ENOMEM;
221
Harald Weltec5478482010-03-18 00:01:43 +0800222 nsvci = htons(nsvc->nsvci);
223 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800224
225 nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh));
226
227 nsh->pdu_type = NS_PDUT_RESET_ACK;
228
Harald Weltebbc9fac2010-05-03 18:54:12 +0200229 DEBUGP(DGPRS, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
230 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800231
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200232 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
233 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800234
Harald Welte24a655f2010-04-30 19:54:29 +0200235 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800236}
237
Harald Welte24a655f2010-04-30 19:54:29 +0200238/* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */
239int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800240{
Harald Welte24a655f2010-04-30 19:54:29 +0200241 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800242 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200243 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200244
245 nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg));
246 if (!nsvc) {
Harald Weltebbc9fac2010-05-03 18:54:12 +0200247 LOGP(DGPRS, LOGL_ERROR, "Unable to resolve NSEI %u "
248 "to NS-VC!\n", msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200249 return -EINVAL;
250 }
Harald Welte9ba50052010-03-14 15:45:01 +0800251
252 nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3);
253 if (!nsh) {
Harald Weltebbc9fac2010-05-03 18:54:12 +0200254 LOGP(DGPRS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800255 return -EIO;
256 }
257
258 nsh->pdu_type = NS_PDUT_UNITDATA;
259 /* spare octet in data[0] */
260 nsh->data[1] = bvci >> 8;
261 nsh->data[2] = bvci & 0xff;
262
Harald Welte24a655f2010-04-30 19:54:29 +0200263 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800264}
265
266/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200267static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800268{
269 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200270 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800271
272 /* spare octet in data[0] */
273 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200274 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200275 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800276
277 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200278 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800279}
280
281/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200282static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800283{
Harald Weltef030b212010-04-26 19:18:54 +0200284 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800285 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200286 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800287 int rc;
288
Harald Weltebbc9fac2010-05-03 18:54:12 +0200289 DEBUGP(DGPRS, "NSEI=%u NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800290
291 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
292
293 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
294 DEBUGPC(DGPRS, "missing cause IE\n");
295 return -EINVAL;
296 }
297
298 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
299 DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause));
300
301 return 0;
302}
303
304/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200305static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800306{
307 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800308 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200309 uint8_t *cause;
310 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800311 int rc;
312
Harald Welte9ba50052010-03-14 15:45:01 +0800313 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0);
314
315 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
316 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
317 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
318 /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200319 LOGP(DGPRS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800320 return -EINVAL;
321 }
322
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200323 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
324 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
325 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800326
Harald Weltebbc9fac2010-05-03 18:54:12 +0200327 DEBUGP(DGPRS, "NSEI=%u NS RESET (NSVCI=%u, cause=%s)\n",
328 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
329
Harald Weltec5478482010-03-18 00:01:43 +0800330 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
331 nsvc->nsei = ntohs(*nsei);
332 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800333
Harald Welte9ba50052010-03-14 15:45:01 +0800334 /* mark the NS-VC as blocked and alive */
Harald Welte9ba50052010-03-14 15:45:01 +0800335 /* start the test procedure */
Harald Welte80405452010-05-03 20:16:13 +0200336 nsvc->timer.cb = gprs_ns_timer_cb;
337 nsvc->timer.data = nsvc;
338 bsc_schedule_timer(&nsvc->timer, NS_TIMER_ALIVE);
Harald Welte9ba50052010-03-14 15:45:01 +0800339
Harald Weltec5478482010-03-18 00:01:43 +0800340 return gprs_ns_tx_reset_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800341}
342
343/* main entry point, here incoming NS frames enter */
Harald Weltef030b212010-04-26 19:18:54 +0200344int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
345 struct sockaddr_in *saddr)
Harald Welte9ba50052010-03-14 15:45:01 +0800346{
347 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltef030b212010-04-26 19:18:54 +0200348 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800349 int rc = 0;
350
Harald Weltef030b212010-04-26 19:18:54 +0200351 /* look up the NSVC based on source address */
352 nsvc = nsvc_by_rem_addr(nsi, saddr);
353 if (!nsvc) {
354 /* Only the RESET procedure creates a new NSVC */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200355 if (nsh->pdu_type != NS_PDUT_RESET) {
356 LOGP(DGPRS, LOGL_INFO, "Ignoring NS PDU type 0x%0x "
357 "from %s for non-existing NS-VC\n",
358 nsh->pdu_type, inet_ntoa(saddr->sin_addr));
Harald Weltef030b212010-04-26 19:18:54 +0200359 return -EIO;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200360 }
361 LOGP(DGPRS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n",
362 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
Harald Weltef030b212010-04-26 19:18:54 +0200363 nsvc = nsvc_create(nsi, 0xffff);
364 nsvc->ip.bts_addr = *saddr;
Harald Weltebbc9fac2010-05-03 18:54:12 +0200365 } else
366 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800367
Harald Welte9ba50052010-03-14 15:45:01 +0800368 switch (nsh->pdu_type) {
369 case NS_PDUT_ALIVE:
370 /* remote end inquires whether we're still alive,
371 * we need to respond with ALIVE_ACK */
Harald Weltec5478482010-03-18 00:01:43 +0800372 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800373 break;
374 case NS_PDUT_ALIVE_ACK:
375 /* stop Tns-alive */
Harald Welte80405452010-05-03 20:16:13 +0200376 bsc_del_timer(&nsvc->timer);
Harald Welte9ba50052010-03-14 15:45:01 +0800377 /* start Tns-test */
Harald Welte80405452010-05-03 20:16:13 +0200378 nsvc->timer_mode = NSVC_TIMER_TNS_TEST;
379 bsc_schedule_timer(&nsvc->timer, NS_TIMER_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +0800380 break;
381 case NS_PDUT_UNITDATA:
382 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200383 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800384 break;
385 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200386 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800387 break;
388 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200389 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800390 break;
391 case NS_PDUT_RESET_ACK:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200392 DEBUGP(DGPRS, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200393 /* mark remote NS-VC as blocked + active */
394 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800395 break;
396 case NS_PDUT_UNBLOCK:
397 /* Section 7.2: unblocking procedure */
Harald Weltebbc9fac2010-05-03 18:54:12 +0200398 DEBUGP(DGPRS, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800399 nsvc->state &= ~NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800400 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800401 break;
402 case NS_PDUT_UNBLOCK_ACK:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200403 DEBUGP(DGPRS, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200404 /* mark remote NS-VC as unblocked + active */
405 nsvc->remote_state = NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800406 break;
407 case NS_PDUT_BLOCK:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200408 DEBUGP(DGPRS, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800409 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec5478482010-03-18 00:01:43 +0800410 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +0800411 break;
412 case NS_PDUT_BLOCK_ACK:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200413 DEBUGP(DGPRS, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +0200414 /* mark remote NS-VC as blocked + active */
415 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +0800416 break;
417 default:
Harald Weltebbc9fac2010-05-03 18:54:12 +0200418 DEBUGP(DGPRS, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
419 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800420 rc = -EINVAL;
421 break;
422 }
423 return rc;
424}
425
Harald Weltef030b212010-04-26 19:18:54 +0200426struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb)
427{
428 struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst);
429
430 nsi->cb = cb;
431 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
432
Harald Welte3771d092010-04-30 20:26:32 +0200433 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +0200434}
435
436void gprs_ns_destroy(struct gprs_ns_inst *nsi)
437{
438 /* FIXME: clear all timers */
439
440 /* recursively free the NSI and all its NSVCs */
441 talloc_free(nsi);
442}
443
444
445/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
446 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
447
448/* Read a single NS-over-IP message */
449static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error,
450 struct sockaddr_in *saddr)
451{
452 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS");
453 int ret = 0;
454 socklen_t saddr_len = sizeof(*saddr);
455
456 if (!msg) {
457 *error = -ENOMEM;
458 return NULL;
459 }
460
461 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0,
462 (struct sockaddr *)saddr, &saddr_len);
463 if (ret < 0) {
Harald Weltebbc9fac2010-05-03 18:54:12 +0200464 LOGP(DGPRS, LOGL_ERROR, "recv error %s during NSIP recv\n",
465 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +0200466 msgb_free(msg);
467 *error = ret;
468 return NULL;
469 } else if (ret == 0) {
470 msgb_free(msg);
471 *error = ret;
472 return NULL;
473 }
474
475 msg->l2h = msg->data;
476 msgb_put(msg, ret);
477
478 return msg;
479}
480
481static int handle_nsip_read(struct bsc_fd *bfd)
482{
483 int error;
484 struct sockaddr_in saddr;
485 struct gprs_ns_inst *nsi = bfd->data;
486 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
487
488 if (!msg)
489 return error;
490
491 return gprs_ns_rcvmsg(nsi, msg, &saddr);
492}
493
494static int handle_nsip_write(struct bsc_fd *bfd)
495{
496 /* FIXME: actually send the data here instead of nsip_sendmsg() */
497 return -EIO;
498}
499
Harald Welte24a655f2010-04-30 19:54:29 +0200500int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +0200501{
502 int rc;
503 struct gprs_ns_inst *nsi = nsvc->nsi;
504 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
505
506 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
507 (struct sockaddr *)daddr, sizeof(*daddr));
508
509 talloc_free(msg);
510
511 return rc;
512}
513
514/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
515static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what)
516{
517 int rc = 0;
518
519 if (what & BSC_FD_READ)
520 rc = handle_nsip_read(bfd);
521 if (what & BSC_FD_WRITE)
522 rc = handle_nsip_write(bfd);
523
524 return rc;
525}
526
527
528/* FIXME: this is currently in input/ipaccess.c */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200529extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port,
Harald Weltef030b212010-04-26 19:18:54 +0200530 int (*cb)(struct bsc_fd *fd, unsigned int what));
531
532/* Listen for incoming GPRS packets */
533int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port)
534{
535 int ret;
536
537 ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb);
538 if (ret < 0)
539 return ret;
540
541 nsi->ll = GPRS_NS_LL_UDP;
542 nsi->nsip.fd.data = nsi;
543
544 return ret;
545}
Harald Welte3771d092010-04-30 20:26:32 +0200546
547/* Establish a connection (from the BSS) to the SGSN */
548struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +0200549 struct sockaddr_in *dest, uint16_t nsei,
550 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +0200551{
552 struct gprs_nsvc *nsvc;
553
554 nsvc = nsvc_by_rem_addr(nsi, dest);
555 if (!nsvc) {
556 nsvc = nsvc_create(nsi, nsvci);
557 nsvc->ip.bts_addr = *dest;
558 }
Harald Welte1203de32010-05-01 11:28:43 +0200559 nsvc->nsei = nsei;
560 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +0200561 nsvc->remote_end_is_sgsn = 1;
562
563 /* Initiate a RESET procedure */
564 if (gprs_ns_tx_simple(nsvc, NS_PDUT_RESET) < 0)
565 return NULL;
566 /* FIXME: should we run a timer and re-transmit the reset request? */
567
568 return nsvc;
569}