blob: 61a96d743883cbecedcc43f2db321f8824483236 [file] [log] [blame]
Harald Weltea9f23c82011-11-23 15:01:31 +01001/* GPRS Networks Service (NS) messages on the Gb interface
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 Welte605ac5d2012-06-16 16:09:52 +08004/* (C) 2009-2012 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
Harald Weltee4cbb3f2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080011 * (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
Harald Weltee4cbb3f2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080017 *
Harald Weltee4cbb3f2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080020 *
21 */
22
Harald Weltea9f23c82011-11-23 15:01:31 +010023/*! \addtogroup libgb
24 * @{
25 */
26
27/*! \file gprs_ns.c */
28
29/*!
30 * GPRS Networks Service (NS) messages on the Gb interface
31 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
32 *
33 * Some introduction into NS: NS is used typically on top of frame relay,
Harald Welte9ba50052010-03-14 15:45:01 +080034 * but in the ip.access world it is encapsulated in UDP packets. It serves
35 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
36 * do much, apart from providing congestion notification and status indication.
37 *
38 * Terms:
39 * NS Network Service
40 * NSVC NS Virtual Connection
41 * NSEI NS Entity Identifier
42 * NSVL NS Virtual Link
43 * NSVLI NS Virtual Link Identifier
44 * BVC BSSGP Virtual Connection
45 * BVCI BSSGP Virtual Connection Identifier
46 * NSVCG NS Virtual Connection Goup
47 * Blocked NS-VC cannot be used for user traffic
48 * Alive Ability of a NS-VC to provide communication
49 *
50 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
51 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
52 * NS then has to firgure out which NSVC's are responsible for this BVCI.
53 * Those mappings are administratively configured.
54 */
55
Harald Weltedd1c83c2010-05-13 13:58:08 +020056/* This implementation has the following limitations:
57 * o Only one NS-VC for each NSE: No load-sharing function
58 * o NSVCI 65535 and 65534 are reserved for internal use
59 * o Only UDP is supported as of now, no frame relay support
60 * o The IP Sub-Network-Service (SNS) as specified in 48.016 is not implemented
61 * o There are no BLOCK and UNBLOCK timers (yet?)
62 */
63
Harald Welte9ba50052010-03-14 15:45:01 +080064#include <stdlib.h>
65#include <unistd.h>
66#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020067#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080068
Holger Hans Peter Freyther3c16de22012-07-12 14:13:41 +020069#include <sys/types.h>
70#include <sys/socket.h>
Harald Welte9ba50052010-03-14 15:45:01 +080071#include <arpa/inet.h>
72
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010073#include <osmocom/core/msgb.h>
74#include <osmocom/gsm/tlv.h>
75#include <osmocom/core/talloc.h>
76#include <osmocom/core/select.h>
77#include <osmocom/core/rate_ctr.h>
Harald Welte73952e32012-06-16 14:59:56 +080078#include <osmocom/core/socket.h>
Harald Welte4fcdd762012-06-16 16:40:42 +080079#include <osmocom/core/signal.h>
Harald Welte73952e32012-06-16 14:59:56 +080080#include <osmocom/gprs/gprs_ns.h>
81#include <osmocom/gprs/gprs_bssgp.h>
82#include <osmocom/gprs/gprs_ns_frgre.h>
83
Harald Weltecca49632012-06-16 17:45:59 +080084#include "common_vty.h"
Harald Welte9ba50052010-03-14 15:45:01 +080085
Harald Welte9ba50052010-03-14 15:45:01 +080086static const struct tlv_definition ns_att_tlvdef = {
87 .def = {
88 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
89 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
90 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
91 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
92 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
93 },
94};
95
Harald Weltec51c23c2010-05-13 12:55:20 +020096enum ns_ctr {
97 NS_CTR_PKTS_IN,
98 NS_CTR_PKTS_OUT,
99 NS_CTR_BYTES_IN,
100 NS_CTR_BYTES_OUT,
101 NS_CTR_BLOCKED,
102 NS_CTR_DEAD,
103};
104
Harald Welte144e0292010-05-13 11:45:07 +0200105static const struct rate_ctr_desc nsvc_ctr_description[] = {
106 { "packets.in", "Packets at NS Level ( In)" },
Harald Weltec51c23c2010-05-13 12:55:20 +0200107 { "packets.out","Packets at NS Level (Out)" },
108 { "bytes.in", "Bytes at NS Level ( In)" },
109 { "bytes.out", "Bytes at NS Level (Out)" },
110 { "blocked", "NS-VC Block count " },
111 { "dead", "NS-VC gone dead count " },
Harald Welte144e0292010-05-13 11:45:07 +0200112};
113
114static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200115 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200116 .group_description = "NSVC Peer Statistics",
117 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
118 .ctr_desc = nsvc_ctr_description,
119};
120
Harald Weltea9f23c82011-11-23 15:01:31 +0100121/*! \brief Lookup struct gprs_nsvc based on NSVCI
122 * \param[in] nsi NS instance in which to search
123 * \param[in] nsvci NSVCI to be searched
124 * \returns gprs_nsvc of respective NSVCI
125 */
Harald Weltef5430362012-06-17 12:25:53 +0800126struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200127{
128 struct gprs_nsvc *nsvc;
129 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
130 if (nsvc->nsvci == nsvci)
131 return nsvc;
132 }
133 return NULL;
134}
135
Harald Weltea9f23c82011-11-23 15:01:31 +0100136/*! \brief Lookup struct gprs_nsvc based on NSEI
137 * \param[in] nsi NS instance in which to search
138 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200139 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100140 */
Harald Weltef5430362012-06-17 12:25:53 +0800141struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200142{
143 struct gprs_nsvc *nsvc;
144 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
145 if (nsvc->nsei == nsei)
146 return nsvc;
147 }
148 return NULL;
149}
150
Jacob Erlbeck69017152013-10-14 12:03:54 +0200151static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
152 uint16_t nsei)
153{
154 struct gprs_nsvc *nsvc;
155 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
156 if (nsvc->nsei == nsei) {
157 if (nsvc->state & NSE_S_BLOCKED ||
158 !(nsvc->state & NSE_S_ALIVE))
159 return nsvc;
160 }
161 }
162 return NULL;
163}
164
Harald Weltef030b212010-04-26 19:18:54 +0200165/* Lookup struct gprs_nsvc based on remote peer socket addr */
166static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
167 struct sockaddr_in *sin)
168{
169 struct gprs_nsvc *nsvc;
170 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000171 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
172 sin->sin_addr.s_addr &&
173 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200174 return nsvc;
175 }
176 return NULL;
177}
178
Harald Welte69a4cf22010-05-03 20:55:10 +0200179static void gprs_ns_timer_cb(void *data);
180
Harald Weltef5430362012-06-17 12:25:53 +0800181struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200182{
183 struct gprs_nsvc *nsvc;
184
Harald Welteb1020d52010-05-25 22:17:30 +0200185 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
186
Harald Weltef030b212010-04-26 19:18:54 +0200187 nsvc = talloc_zero(nsi, struct gprs_nsvc);
188 nsvc->nsvci = nsvci;
189 /* before RESET procedure: BLOCKED and DEAD */
190 nsvc->state = NSE_S_BLOCKED;
191 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200192 nsvc->timer.cb = gprs_ns_timer_cb;
193 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200194 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200195
Harald Weltef030b212010-04-26 19:18:54 +0200196 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
197
198 return nsvc;
199}
Harald Welte9ba50052010-03-14 15:45:01 +0800200
Harald Weltea9f23c82011-11-23 15:01:31 +0100201/*! \brief Delete given NS-VC
202 * \param[in] nsvc gprs_nsvc to be deleted
203 */
Harald Weltef5430362012-06-17 12:25:53 +0800204void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000205{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200206 if (osmo_timer_pending(&nsvc->timer))
207 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000208 llist_del(&nsvc->list);
209 talloc_free(nsvc);
210}
211
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200212static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200213 uint8_t cause)
214{
215 struct ns_signal_data nssd;
216
217 nssd.nsvc = nsvc;
218 nssd.cause = cause;
219
Harald Welte4fcdd762012-06-16 16:40:42 +0800220 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200221}
222
Harald Welte9ba50052010-03-14 15:45:01 +0800223/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200224static const struct value_string ns_cause_str[] = {
225 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
226 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
227 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
228 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
229 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
230 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
231 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
232 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200233 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200234 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
235 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
236 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800237};
238
Harald Weltea9f23c82011-11-23 15:01:31 +0100239/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200240const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800241{
Harald Welte2577d412010-05-02 09:37:45 +0200242 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800243}
244
Harald Welte24a655f2010-04-30 19:54:29 +0200245static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200246extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200247
Harald Welte24a655f2010-04-30 19:54:29 +0200248static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800249{
Harald Weltef030b212010-04-26 19:18:54 +0200250 int ret;
251
Harald Weltecca49632012-06-16 17:45:59 +0800252 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200253
Harald Welte144e0292010-05-13 11:45:07 +0200254 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200255 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
256 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200257
Harald Welteb3ee2652010-05-19 14:38:50 +0200258 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200259 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200260 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200261 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200262 case GPRS_NS_LL_FR_GRE:
263 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
264 break;
Harald Weltef030b212010-04-26 19:18:54 +0200265 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200266 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200267 msgb_free(msg);
268 ret = -EIO;
269 break;
270 }
271 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800272}
273
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200274static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800275{
Harald Welteba4c6662010-05-19 15:38:10 +0200276 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800277 struct gprs_ns_hdr *nsh;
278
Harald Weltecca49632012-06-16 17:45:59 +0800279 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200280
Harald Welte9ba50052010-03-14 15:45:01 +0800281 if (!msg)
282 return -ENOMEM;
283
Harald Welte144e0292010-05-13 11:45:07 +0200284 msg->l2h = msgb_put(msg, sizeof(*nsh));
285 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800286
287 nsh->pdu_type = pdu_type;
288
Harald Welte24a655f2010-04-30 19:54:29 +0200289 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800290}
291
Harald Weltea9f23c82011-11-23 15:01:31 +0100292/*! \brief Transmit a NS-RESET on a given NSVC
293 * \param[in] nsvc NS-VC used for transmission
294 * \paam[in] cause Numeric NS cause value
295 */
Harald Welte834f26d2010-05-11 06:20:54 +0200296int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200297{
Harald Welteba4c6662010-05-19 15:38:10 +0200298 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200299 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200300 uint16_t nsvci = htons(nsvc->nsvci);
301 uint16_t nsei = htons(nsvc->nsei);
302
Harald Weltecca49632012-06-16 17:45:59 +0800303 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200304
Harald Welte69a4cf22010-05-03 20:55:10 +0200305 if (!msg)
306 return -ENOMEM;
307
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000308 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
309 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
310
Harald Welte144e0292010-05-13 11:45:07 +0200311 msg->l2h = msgb_put(msg, sizeof(*nsh));
312 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200313 nsh->pdu_type = NS_PDUT_RESET;
314
315 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
316 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
317 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
318
319 return gprs_ns_tx(nsvc, msg);
320
321}
322
Harald Weltea9f23c82011-11-23 15:01:31 +0100323/*! \brief Transmit a NS-STATUS on a given NSVC
324 * \param[in] nsvc NS-VC to be used for transmission
325 * \param[in] cause Numeric NS cause value
326 * \param[in] bvci BVCI to be reset within NSVC
327 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200328int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
329 uint16_t bvci, struct msgb *orig_msg)
330{
Harald Welteba4c6662010-05-19 15:38:10 +0200331 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200332 struct gprs_ns_hdr *nsh;
333 uint16_t nsvci = htons(nsvc->nsvci);
334
Harald Weltecca49632012-06-16 17:45:59 +0800335 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200336
Harald Weltec1402a62010-05-12 11:48:44 +0200337 bvci = htons(bvci);
338
339 if (!msg)
340 return -ENOMEM;
341
Harald Welte90af1942010-05-15 23:02:24 +0200342 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000343 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
344
Harald Welte144e0292010-05-13 11:45:07 +0200345 msg->l2h = msgb_put(msg, sizeof(*nsh));
346 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200347 nsh->pdu_type = NS_PDUT_STATUS;
348
349 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
350
351 /* Section 9.2.7.1: Static conditions for NS-VCI */
352 if (cause == NS_CAUSE_NSVC_BLOCKED ||
353 cause == NS_CAUSE_NSVC_UNKNOWN)
354 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
355
356 /* Section 9.2.7.2: Static conditions for NS PDU */
357 switch (cause) {
358 case NS_CAUSE_SEM_INCORR_PDU:
359 case NS_CAUSE_PDU_INCOMP_PSTATE:
360 case NS_CAUSE_PROTO_ERR_UNSPEC:
361 case NS_CAUSE_INVAL_ESSENT_IE:
362 case NS_CAUSE_MISSING_ESSENT_IE:
363 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
364 orig_msg->l2h);
365 break;
366 default:
367 break;
368 }
369
370 /* Section 9.2.7.3: Static conditions for BVCI */
371 if (cause == NS_CAUSE_BVCI_UNKNOWN)
372 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
373
374 return gprs_ns_tx(nsvc, msg);
375}
376
Harald Weltea9f23c82011-11-23 15:01:31 +0100377/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
378 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
379 * \param[in] cause Numeric NS Cause value
380 * \returns 0 in case of success
381 */
Harald Welte834f26d2010-05-11 06:20:54 +0200382int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
383{
Harald Welteba4c6662010-05-19 15:38:10 +0200384 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200385 struct gprs_ns_hdr *nsh;
386 uint16_t nsvci = htons(nsvc->nsvci);
387
Harald Weltecca49632012-06-16 17:45:59 +0800388 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200389
Harald Welte834f26d2010-05-11 06:20:54 +0200390 if (!msg)
391 return -ENOMEM;
392
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000393 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
394 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
395
Harald Welte834f26d2010-05-11 06:20:54 +0200396 /* be conservative and mark it as blocked even now! */
397 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200398 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200399
Harald Welte144e0292010-05-13 11:45:07 +0200400 msg->l2h = msgb_put(msg, sizeof(*nsh));
401 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200402 nsh->pdu_type = NS_PDUT_BLOCK;
403
404 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
405 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
406
407 return gprs_ns_tx(nsvc, msg);
408}
409
Harald Weltea9f23c82011-11-23 15:01:31 +0100410/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
411 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
412 * \returns 0 in case of success
413 */
Harald Welte834f26d2010-05-11 06:20:54 +0200414int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
415{
Harald Weltecca49632012-06-16 17:45:59 +0800416 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000417 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
418 nsvc->nsei, nsvc->nsvci);
419
Harald Welte834f26d2010-05-11 06:20:54 +0200420 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
421}
422
Harald Weltea9f23c82011-11-23 15:01:31 +0100423/*! \brief Transmit a NS-ALIVE on a given NS-VC
424 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
425 * \returns 0 in case of success
426 */
Harald Welteb983c312010-05-12 12:11:45 +0000427int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
428{
Harald Weltecca49632012-06-16 17:45:59 +0800429 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000430 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
431 nsvc->nsei, nsvc->nsvci);
432
433 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
434}
435
Harald Weltea9f23c82011-11-23 15:01:31 +0100436/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
437 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
438 * \returns 0 in case of success
439 */
Harald Welteb983c312010-05-12 12:11:45 +0000440int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
441{
Harald Weltecca49632012-06-16 17:45:59 +0800442 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000443 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
444 nsvc->nsei, nsvc->nsvci);
445
446 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
447}
448
Harald Weltefe4ab902010-05-12 17:19:53 +0000449static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
450 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
451 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
452 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200453};
454
Harald Welte7c24b9e2010-05-12 12:21:52 +0000455static const struct value_string timer_mode_strs[] = {
456 { NSVC_TIMER_TNS_RESET, "tns-reset" },
457 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
458 { NSVC_TIMER_TNS_TEST, "tns-test" },
459 { 0, NULL }
460};
461
Harald Welte69a4cf22010-05-03 20:55:10 +0200462static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
463{
Harald Weltefe4ab902010-05-12 17:19:53 +0000464 enum ns_timeout tout = timer_mode_tout[mode];
465 unsigned int seconds = nsvc->nsi->timeout[tout];
466
Harald Weltecca49632012-06-16 17:45:59 +0800467 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000468 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
469 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000470 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000471
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200472 if (osmo_timer_pending(&nsvc->timer))
473 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200474
475 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200476 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200477}
478
Harald Welte80405452010-05-03 20:16:13 +0200479static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800480{
481 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000482 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
483 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800484
Harald Weltecca49632012-06-16 17:45:59 +0800485 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000486 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
487 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000488 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000489
Harald Welte80405452010-05-03 20:16:13 +0200490 switch (nsvc->timer_mode) {
491 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800492 /* Tns-alive case: we expired without response ! */
493 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000494 if (nsvc->alive_retries >
495 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800496 /* mark as dead and blocked */
497 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200498 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
499 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200500 LOGP(DNS, LOGL_NOTICE,
501 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200502 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000503 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200504 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
505 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800506 return;
507 }
Harald Welteb983c312010-05-12 12:11:45 +0000508 /* Tns-test case: send NS-ALIVE PDU */
509 gprs_ns_tx_alive(nsvc);
510 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200511 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200512 break;
513 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800514 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000515 gprs_ns_tx_alive(nsvc);
516 /* start Tns-alive timer (transition into faster
517 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000518 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200519 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
520 break;
521 case NSVC_TIMER_TNS_RESET:
522 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200523 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200524 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200525 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200526 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200527 case _NSVC_TIMER_NR:
528 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800529 }
Harald Welte9ba50052010-03-14 15:45:01 +0800530}
531
532/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800533static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800534{
Harald Welteba4c6662010-05-19 15:38:10 +0200535 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800536 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200537 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800538
Harald Weltecca49632012-06-16 17:45:59 +0800539 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800540 if (!msg)
541 return -ENOMEM;
542
Harald Weltec5478482010-03-18 00:01:43 +0800543 nsvci = htons(nsvc->nsvci);
544 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800545
Harald Welte144e0292010-05-13 11:45:07 +0200546 msg->l2h = msgb_put(msg, sizeof(*nsh));
547 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800548
549 nsh->pdu_type = NS_PDUT_RESET_ACK;
550
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200551 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200552 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800553
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200554 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
555 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800556
Harald Welte24a655f2010-04-30 19:54:29 +0200557 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800558}
559
Harald Weltea9f23c82011-11-23 15:01:31 +0100560/*! \brief High-level function for transmitting a NS-UNITDATA messsage
561 * \param[in] nsi NS-instance on which we shall transmit
562 * \param[in] msg struct msgb to be trasnmitted
563 *
564 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
565 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
566 * header for the NS-UNITDATA message type and sends it off.
567 *
568 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
569 */
Harald Welte24a655f2010-04-30 19:54:29 +0200570int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800571{
Harald Welte24a655f2010-04-30 19:54:29 +0200572 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800573 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200574 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200575
Jacob Erlbeck69017152013-10-14 12:03:54 +0200576 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200577 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200578 int rc;
579 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
580 LOGP(DNS, LOGL_ERROR,
581 "All NS-VCs for NSEI %u are either dead or blocked!\n",
582 msgb_nsei(msg));
583 rc = -EBUSY;
584 } else {
585 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
586 "to NS-VC!\n", msgb_nsei(msg));
587 rc = -EINVAL;
588 }
589
Harald Weltef47df642010-08-09 21:15:40 +0800590 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200591 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200592 }
Harald Weltecca49632012-06-16 17:45:59 +0800593 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800594
Harald Welteec20ba42010-05-13 12:18:49 +0200595 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
596 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800597 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200598 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800599 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800600 return -EIO;
601 }
602
603 nsh->pdu_type = NS_PDUT_UNITDATA;
604 /* spare octet in data[0] */
605 nsh->data[1] = bvci >> 8;
606 nsh->data[2] = bvci & 0xff;
607
Harald Welte24a655f2010-04-30 19:54:29 +0200608 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800609}
610
611/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200612static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800613{
614 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200615 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800616
Harald Weltec1402a62010-05-12 11:48:44 +0200617 if (nsvc->state & NSE_S_BLOCKED)
618 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
619 0, msg);
620
Harald Welte9ba50052010-03-14 15:45:01 +0800621 /* spare octet in data[0] */
622 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200623 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200624 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800625
626 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200627 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800628}
629
630/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200631static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800632{
Harald Weltef030b212010-04-26 19:18:54 +0200633 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800634 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200635 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800636 int rc;
637
Harald Welte41337832010-05-16 00:24:26 +0200638 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800639
Harald Weltebd33f3d2010-05-30 17:19:38 +0200640 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
641 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200642 if (rc < 0) {
643 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
644 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
645 "Error during TLV Parse\n", nsvc->nsei);
646 return rc;
647 }
Harald Welte9ba50052010-03-14 15:45:01 +0800648
649 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200650 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800651 return -EINVAL;
652 }
653
654 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200655 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800656
657 return 0;
658}
659
660/* Section 7.3 */
Harald Welte24a655f2010-04-30 19:54:29 +0200661static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800662{
663 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800664 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200665 uint8_t *cause;
666 uint16_t *nsvci, *nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800667 int rc;
668
Harald Weltebd33f3d2010-05-30 17:19:38 +0200669 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
670 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200671 if (rc < 0) {
672 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
673 "Error during TLV Parse\n", nsvc->nsei);
674 return rc;
675 }
Harald Welte9ba50052010-03-14 15:45:01 +0800676
677 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
678 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
679 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200680 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200681 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800682 return -EINVAL;
683 }
684
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200685 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
686 nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
687 nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI);
Harald Welte9ba50052010-03-14 15:45:01 +0800688
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200689 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200690 nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause));
691
Harald Weltec1402a62010-05-12 11:48:44 +0200692 /* Mark NS-VC as blocked and alive */
Harald Weltec5478482010-03-18 00:01:43 +0800693 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200694
Harald Weltec5478482010-03-18 00:01:43 +0800695 nsvc->nsei = ntohs(*nsei);
696 nsvc->nsvci = ntohs(*nsvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800697
Harald Welte834f26d2010-05-11 06:20:54 +0200698 /* inform interested parties about the fact that this NSVC
699 * has received RESET */
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200700 ns_osmo_signal_dispatch(nsvc, S_NS_RESET, *cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200701
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200702 rc = gprs_ns_tx_reset_ack(nsvc);
703
704 /* start the test procedure */
705 gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
706 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
707
708 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800709}
710
Harald Welte834f26d2010-05-11 06:20:54 +0200711static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
712{
713 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
714 struct tlv_parsed tp;
715 uint8_t *cause;
716 int rc;
717
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200718 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200719
720 nsvc->state |= NSE_S_BLOCKED;
721
Harald Weltebd33f3d2010-05-30 17:19:38 +0200722 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
723 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200724 if (rc < 0) {
725 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
726 "Error during TLV Parse\n", nsvc->nsei);
727 return rc;
728 }
Harald Welte834f26d2010-05-11 06:20:54 +0200729
730 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
731 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200732 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200733 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200734 return -EINVAL;
735 }
736
737 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
738 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
739
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200740 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +0200741 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200742
743 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
744}
745
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200746int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
747 struct gprs_nsvc *fallback_nsvc,
748 struct gprs_nsvc **new_nsvc);
749
750int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
751 struct gprs_nsvc *nsvc);
752
Harald Weltea9f23c82011-11-23 15:01:31 +0100753/*! \brief Receive incoming NS message from underlying transport layer
754 * \param nsi NS instance to which the data belongs
755 * \param[in] msg message buffer containing newly-received data
756 * \param[in] saddr socketaddr from which data was received
757 * \param[in] ll link-layer type in which data was received
758 * \returns 0 in case of success, < 0 in case of error
759 *
760 * This is the main entry point int othe NS imlementation where frames
761 * from the underlying transport (normally UDP) enter.
762 */
Harald Weltef030b212010-04-26 19:18:54 +0200763int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200764 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800765{
Harald Weltef030b212010-04-26 19:18:54 +0200766 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800767 int rc = 0;
768
Harald Weltef030b212010-04-26 19:18:54 +0200769 /* look up the NSVC based on source address */
770 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200771
Harald Weltef030b212010-04-26 19:18:54 +0200772 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200773 struct gprs_nsvc *fallback_nsvc;
774
775 fallback_nsvc = nsi->unknown_nsvc;
776 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
777 fallback_nsvc->ip.bts_addr = *saddr;
778 fallback_nsvc->ll = ll;
779
780 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
781
782 switch (rc) {
783 case GPRS_NS_CS_CREATED:
784 case GPRS_NS_CS_FOUND:
Harald Welteb3ee2652010-05-19 14:38:50 +0200785 nsvc->ll = ll;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200786 break;
787 case GPRS_NS_CS_SKIPPED:
788 case GPRS_NS_CS_REJECTED:
789 break;
790 default:
Harald Welte36250382010-05-28 10:08:14 +0200791 return rc;
792 }
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200793
794 rc = 0;
795 }
796
797 if (nsvc) {
Harald Welte2b7d4652010-05-11 18:40:45 +0200798 nsvc->ip.bts_addr = *saddr;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200799 rc = gprs_ns_process_msg(nsi, msg, nsvc);
800 }
801
802 return rc;
803}
804
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200805const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200806{
807 static char buf[80];
808 snprintf(buf, sizeof(buf), "%s:%u",
809 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
810 ntohs(nsvc->ip.bts_addr.sin_port));
811
812 return buf;
813}
814
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200815void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
816{
817 nsvc->ll = other->ll;
818
819 switch (nsvc->ll) {
820 case GPRS_NS_LL_UDP:
821 nsvc->ip = other->ip;
822 break;
823 case GPRS_NS_LL_FR_GRE:
824 nsvc->frgre = other->frgre;
825 break;
826 default:
827 break;
828 }
829}
830
831void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
832{
833 switch (nsvc->ll) {
834 case GPRS_NS_LL_UDP:
835 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
836 nsvc->ip.bts_addr.sin_port = 0;
837 break;
838 case GPRS_NS_LL_FR_GRE:
839 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
840 nsvc->frgre.bts_addr.sin_port = 0;
841 break;
842 default:
843 break;
844 }
845}
846
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200847/*! \brief Create/get NS-VC independently from underlying transport layer
848 * \param nsi NS instance to which the data belongs
849 * \param[in] msg message buffer containing newly-received data
850 * \param[in] fallback_nsvc is used to send error messages back to the peer
851 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
852 * been created or found
853 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
854 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
855 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
856 * has been created and registered, and GPRS_NS_CS_FOUND if an
857 * existing NS-VC object has been found with the same NSEI.
858 *
859 * This contains the initial NS automaton state (NS-VC not yet attached).
860 */
861int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
862 struct gprs_nsvc *fallback_nsvc,
863 struct gprs_nsvc **new_nsvc)
864{
865 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
866 struct gprs_nsvc *existing_nsvc;
867
868 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200869 uint16_t nsvci;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200870
871 int rc;
872
873 if (nsh->pdu_type == NS_PDUT_STATUS) {
874 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
875 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200876 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200877 return GPRS_NS_CS_SKIPPED;
878 }
879
880 /* Only the RESET procedure creates a new NSVC */
881 if (nsh->pdu_type != NS_PDUT_RESET) {
882 /* Since we have no NSVC, we have to use a fake */
883 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
884 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
885 "from %s for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200886 nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200887 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
888 fallback_nsvc->state = NSE_S_ALIVE;
889
890 rc = gprs_ns_tx_status(fallback_nsvc,
891 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
892 if (rc < 0) {
893 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200894 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200895 return rc;
896 }
897 return GPRS_NS_CS_REJECTED;
898 }
899
900 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
901 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
902 if (rc < 0) {
903 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
904 "TLV Parse\n", rc);
905 return rc;
906 }
907 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
908 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
909 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
910 gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
911 msg);
912 return -EINVAL;
913 }
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200914 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
915 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200916 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200917 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200918 if (!existing_nsvc) {
919 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
920 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
921 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200922 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200923
924 return GPRS_NS_CS_CREATED;
925 }
926
927 *new_nsvc = existing_nsvc;
928 return GPRS_NS_CS_FOUND;
929}
930
931/*! \brief Process NS message independently from underlying transport layer
932 * \param nsi NS instance to which the data belongs
933 * \param[in] msg message buffer containing newly-received data
934 * \param[in] nsvc refers to the virtual connection
935 * \returns 0 in case of success, < 0 in case of error
936 *
937 * This contains the main NS automaton.
938 */
939int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
940 struct gprs_nsvc *nsvc)
941{
942 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
943 int rc = 0;
944
945 msgb_nsei(msg) = nsvc->nsei;
Harald Weltec5478482010-03-18 00:01:43 +0800946
Harald Weltecca49632012-06-16 17:45:59 +0800947 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200948
Harald Welte144e0292010-05-13 11:45:07 +0200949 /* Increment number of Incoming bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200950 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
Harald Weltec51c23c2010-05-13 12:55:20 +0200951 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200952
Harald Welte9ba50052010-03-14 15:45:01 +0800953 switch (nsh->pdu_type) {
954 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +0000955 /* If we're dead and blocked and suddenly receive a
956 * NS-ALIVE out of the blue, we might have been re-started
957 * and should send a NS-RESET to make sure everything recovers
958 * fine. */
959 if (nsvc->state == NSE_S_BLOCKED)
960 rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE);
961 else
962 rc = gprs_ns_tx_alive_ack(nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800963 break;
964 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +0200965 /* stop Tns-alive and start Tns-test */
Harald Welte69a4cf22010-05-03 20:55:10 +0200966 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte7fb7e612010-05-03 21:11:22 +0200967 if (nsvc->remote_end_is_sgsn) {
968 /* FIXME: this should be one level higher */
969 if (nsvc->state & NSE_S_BLOCKED)
Harald Weltec1402a62010-05-12 11:48:44 +0200970 rc = gprs_ns_tx_unblock(nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +0200971 }
Harald Welte9ba50052010-03-14 15:45:01 +0800972 break;
973 case NS_PDUT_UNITDATA:
974 /* actual user data */
Harald Welte24a655f2010-04-30 19:54:29 +0200975 rc = gprs_ns_rx_unitdata(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800976 break;
977 case NS_PDUT_STATUS:
Harald Welte24a655f2010-04-30 19:54:29 +0200978 rc = gprs_ns_rx_status(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800979 break;
980 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +0200981 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800982 break;
983 case NS_PDUT_RESET_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200984 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +0200985 /* mark NS-VC as blocked + active */
986 nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +0200987 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltec51c23c2010-05-13 12:55:20 +0200988 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte731d1fc2010-05-14 11:53:08 +0000989 if (nsvc->persistent || nsvc->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +0200990 /* stop RESET timer */
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200991 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200992 }
Harald Welte90af1942010-05-15 23:02:24 +0200993 /* Initiate TEST proc.: Send ALIVE and start timer */
994 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
Harald Weltec9531202010-05-28 09:58:27 +0200995 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +0800996 break;
997 case NS_PDUT_UNBLOCK:
998 /* Section 7.2: unblocking procedure */
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200999 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +08001000 nsvc->state &= ~NSE_S_BLOCKED;
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001001 ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0);
Harald Weltec5478482010-03-18 00:01:43 +08001002 rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001003 break;
1004 case NS_PDUT_UNBLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001005 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001006 /* mark NS-VC as unblocked + active */
1007 nsvc->state = NSE_S_ALIVE;
Harald Weltef030b212010-04-26 19:18:54 +02001008 nsvc->remote_state = NSE_S_ALIVE;
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001009 ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001010 break;
1011 case NS_PDUT_BLOCK:
Harald Welte834f26d2010-05-11 06:20:54 +02001012 rc = gprs_ns_rx_block(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001013 break;
1014 case NS_PDUT_BLOCK_ACK:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001015 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001016 /* mark remote NS-VC as blocked + active */
1017 nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001018 break;
1019 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001020 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001021 nsvc->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001022 rc = -EINVAL;
1023 break;
1024 }
1025 return rc;
1026}
1027
Harald Weltea9f23c82011-11-23 15:01:31 +01001028/*! \brief Create a new GPRS NS instance
1029 * \param[in] cb Call-back function for incoming BSSGP data
1030 * \returns dynamically allocated gprs_ns_inst
1031 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001032struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001033{
Harald Welte4fcdd762012-06-16 16:40:42 +08001034 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001035
1036 nsi->cb = cb;
1037 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001038 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1039 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1040 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1041 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1042 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1043 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1044 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001045
Harald Welte60cc6ac2010-05-13 14:20:56 +02001046 /* Create the dummy NSVC that we use for sending
1047 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001048 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +02001049 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001050
Harald Welte3771d092010-04-30 20:26:32 +02001051 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001052}
1053
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001054void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001055{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001056 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001057
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001058 /* delete all NSVCs and clear their timers */
1059 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1060 gprs_nsvc_delete(nsvc);
1061
1062 /* close socket and unregister */
1063 if (nsi->nsip.fd.data) {
1064 close(nsi->nsip.fd.fd);
1065 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001066 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001067 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001068}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001069
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001070/*! \brief Destroy an entire NS instance
1071 * \param nsi gprs_ns_inst that is to be destroyed
1072 *
1073 * This function releases all resources associated with the
1074 * NS-instance.
1075 */
1076void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1077{
1078 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001079 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001080 talloc_free(nsi);
1081}
1082
1083
1084/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1085 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1086
1087/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001088static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001089 struct sockaddr_in *saddr)
1090{
Harald Welteba4c6662010-05-19 15:38:10 +02001091 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001092 int ret = 0;
1093 socklen_t saddr_len = sizeof(*saddr);
1094
1095 if (!msg) {
1096 *error = -ENOMEM;
1097 return NULL;
1098 }
1099
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001100 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001101 (struct sockaddr *)saddr, &saddr_len);
1102 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001103 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001104 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001105 msgb_free(msg);
1106 *error = ret;
1107 return NULL;
1108 } else if (ret == 0) {
1109 msgb_free(msg);
1110 *error = ret;
1111 return NULL;
1112 }
1113
1114 msg->l2h = msg->data;
1115 msgb_put(msg, ret);
1116
1117 return msg;
1118}
1119
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001120static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001121{
1122 int error;
1123 struct sockaddr_in saddr;
1124 struct gprs_ns_inst *nsi = bfd->data;
1125 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1126
1127 if (!msg)
1128 return error;
1129
Harald Welteb3ee2652010-05-19 14:38:50 +02001130 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001131
1132 msgb_free(msg);
1133
1134 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001135}
1136
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001137static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001138{
1139 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1140 return -EIO;
1141}
1142
Harald Welteb3ee2652010-05-19 14:38:50 +02001143static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001144{
1145 int rc;
1146 struct gprs_ns_inst *nsi = nsvc->nsi;
1147 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1148
1149 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1150 (struct sockaddr *)daddr, sizeof(*daddr));
1151
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001152 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001153
1154 return rc;
1155}
1156
1157/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001158static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001159{
1160 int rc = 0;
1161
1162 if (what & BSC_FD_READ)
1163 rc = handle_nsip_read(bfd);
1164 if (what & BSC_FD_WRITE)
1165 rc = handle_nsip_write(bfd);
1166
1167 return rc;
1168}
1169
Harald Weltea9f23c82011-11-23 15:01:31 +01001170/*! \brief Create a listening socket for GPRS NS/UDP/IP
1171 * \param[in] nsi NS protocol instance to listen
1172 * \returns >=0 (fd) in case of success, negative in case of error
1173 *
1174 * A call to this function will create a UDP socket bound to the port
1175 * number and IP address specified in the NS protocol instance. The
1176 * file descriptor of the socket will be stored in nsi->nsip.fd.
1177 */
Harald Welte7fb05232010-05-19 15:09:09 +02001178int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001179{
Harald Welte73952e32012-06-16 14:59:56 +08001180 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001181 int ret;
1182
Harald Welte73952e32012-06-16 14:59:56 +08001183 in.s_addr = htonl(nsi->nsip.local_ip);
1184
1185 nsi->nsip.fd.cb = nsip_fd_cb;
1186 nsi->nsip.fd.data = nsi;
1187 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1188 IPPROTO_UDP, inet_ntoa(in),
1189 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001190 if (ret < 0)
1191 return ret;
1192
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001193 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1194 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1195 if (ret < 0)
1196 LOGP(DNS, LOGL_ERROR,
1197 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1198 nsi->nsip.dscp, ret, errno);
1199
Harald Weltef030b212010-04-26 19:18:54 +02001200
1201 return ret;
1202}
Harald Welte3771d092010-04-30 20:26:32 +02001203
Harald Weltea9f23c82011-11-23 15:01:31 +01001204/*! \brief Initiate a RESET procedure
1205 * \param[in] nsvc NS-VC in which to start the procedure
1206 * \param[in] cause Numeric NS cause value
1207 *
1208 * This is a high-level function initiating a NS-RESET procedure. It
1209 * will not only send a NS-RESET, but also set the state to BLOCKED and
1210 * start the Tns-reset timer.
1211 */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001212void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001213{
Harald Welteb1020d52010-05-25 22:17:30 +02001214 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1215 nsvc->nsei);
1216
Harald Welte731d1fc2010-05-14 11:53:08 +00001217 /* Mark NS-VC locally as blocked and dead */
1218 nsvc->state = NSE_S_BLOCKED;
1219 /* Send NS-RESET PDU */
1220 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
1221 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1222 nsvc->nsei);
1223 }
1224 /* Start Tns-reset */
1225 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +00001226}
1227
Harald Weltea9f23c82011-11-23 15:01:31 +01001228/*! \brief Establish a NS connection (from the BSS) to the SGSN
1229 * \param nsi NS-instance
1230 * \param[in] dest Destination IP/Port
1231 * \param[in] nsei NSEI of the to-be-established NS-VC
1232 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1233 * \returns struct gprs_nsvc representing the new NS-VC
1234 *
1235 * This function will establish a single NS/UDP/IP connection in uplink
1236 * (BSS to SGSN) direction.
1237 */
Harald Weltef5430362012-06-17 12:25:53 +08001238struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001239 struct sockaddr_in *dest, uint16_t nsei,
1240 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001241{
1242 struct gprs_nsvc *nsvc;
1243
1244 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001245 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001246 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001247 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001248 nsvc->nsei = nsei;
1249 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +02001250 nsvc->remote_end_is_sgsn = 1;
1251
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001252 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1253 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001254}
Harald Weltea9f23c82011-11-23 15:01:31 +01001255
Harald Weltecca49632012-06-16 17:45:59 +08001256void gprs_ns_set_log_ss(int ss)
1257{
1258 DNS = ss;
1259}
1260
Harald Weltea9f23c82011-11-23 15:01:31 +01001261/*! }@ */