blob: 6a35ad35c140c49eaca54284ab0dc849119882ec [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,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200103 NS_CTR_REPLACED,
104 NS_CTR_NSEI_CHG,
Harald Weltec51c23c2010-05-13 12:55:20 +0200105};
106
Harald Welte144e0292010-05-13 11:45:07 +0200107static const struct rate_ctr_desc nsvc_ctr_description[] = {
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200108 { "packets.in", "Packets at NS Level ( In)" },
109 { "packets.out","Packets at NS Level (Out)" },
110 { "bytes.in", "Bytes at NS Level ( In)" },
111 { "bytes.out", "Bytes at NS Level (Out)" },
112 { "blocked", "NS-VC Block count " },
113 { "dead", "NS-VC gone dead count " },
114 { "replaced", "NS-VC replaced other count" },
115 { "nsei-chg", "NS-VC changed NSEI " },
Harald Welte144e0292010-05-13 11:45:07 +0200116};
117
118static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200119 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200120 .group_description = "NSVC Peer Statistics",
121 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
122 .ctr_desc = nsvc_ctr_description,
123};
124
Harald Weltea9f23c82011-11-23 15:01:31 +0100125/*! \brief Lookup struct gprs_nsvc based on NSVCI
126 * \param[in] nsi NS instance in which to search
127 * \param[in] nsvci NSVCI to be searched
128 * \returns gprs_nsvc of respective NSVCI
129 */
Harald Weltef5430362012-06-17 12:25:53 +0800130struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200131{
132 struct gprs_nsvc *nsvc;
133 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
134 if (nsvc->nsvci == nsvci)
135 return nsvc;
136 }
137 return NULL;
138}
139
Harald Weltea9f23c82011-11-23 15:01:31 +0100140/*! \brief Lookup struct gprs_nsvc based on NSEI
141 * \param[in] nsi NS instance in which to search
142 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200143 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100144 */
Harald Weltef5430362012-06-17 12:25:53 +0800145struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200146{
147 struct gprs_nsvc *nsvc;
148 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
149 if (nsvc->nsei == nsei)
150 return nsvc;
151 }
152 return NULL;
153}
154
Jacob Erlbeck69017152013-10-14 12:03:54 +0200155static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
156 uint16_t nsei)
157{
158 struct gprs_nsvc *nsvc;
159 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
160 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200161 if (!(nsvc->state & NSE_S_BLOCKED) &&
162 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200163 return nsvc;
164 }
165 }
166 return NULL;
167}
168
Harald Weltef030b212010-04-26 19:18:54 +0200169/* Lookup struct gprs_nsvc based on remote peer socket addr */
170static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
171 struct sockaddr_in *sin)
172{
173 struct gprs_nsvc *nsvc;
174 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000175 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
176 sin->sin_addr.s_addr &&
177 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200178 return nsvc;
179 }
180 return NULL;
181}
182
Harald Welte69a4cf22010-05-03 20:55:10 +0200183static void gprs_ns_timer_cb(void *data);
184
Harald Weltef5430362012-06-17 12:25:53 +0800185struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200186{
187 struct gprs_nsvc *nsvc;
188
Harald Welteb1020d52010-05-25 22:17:30 +0200189 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
190
Harald Weltef030b212010-04-26 19:18:54 +0200191 nsvc = talloc_zero(nsi, struct gprs_nsvc);
192 nsvc->nsvci = nsvci;
193 /* before RESET procedure: BLOCKED and DEAD */
194 nsvc->state = NSE_S_BLOCKED;
195 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200196 nsvc->timer.cb = gprs_ns_timer_cb;
197 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200198 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200199
Harald Weltef030b212010-04-26 19:18:54 +0200200 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
201
202 return nsvc;
203}
Harald Welte9ba50052010-03-14 15:45:01 +0800204
Harald Weltea9f23c82011-11-23 15:01:31 +0100205/*! \brief Delete given NS-VC
206 * \param[in] nsvc gprs_nsvc to be deleted
207 */
Harald Weltef5430362012-06-17 12:25:53 +0800208void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000209{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200210 if (osmo_timer_pending(&nsvc->timer))
211 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000212 llist_del(&nsvc->list);
213 talloc_free(nsvc);
214}
215
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200216static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200217 uint8_t cause)
218{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200219 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200220
221 nssd.nsvc = nsvc;
222 nssd.cause = cause;
223
Harald Welte4fcdd762012-06-16 16:40:42 +0800224 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200225}
226
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200227static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
228{
229 struct ns_signal_data nssd = {0};
230
231 nssd.nsvc = nsvc;
232 nssd.old_nsvc = old_nsvc;
233
234 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
235}
236
Harald Welte9ba50052010-03-14 15:45:01 +0800237/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200238static const struct value_string ns_cause_str[] = {
239 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
240 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
241 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
242 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
243 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
244 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
245 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
246 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200247 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200248 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
249 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
250 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800251};
252
Harald Weltea9f23c82011-11-23 15:01:31 +0100253/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200254const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800255{
Harald Welte2577d412010-05-02 09:37:45 +0200256 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800257}
258
Harald Welte24a655f2010-04-30 19:54:29 +0200259static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200260extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200261
Harald Welte24a655f2010-04-30 19:54:29 +0200262static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800263{
Harald Weltef030b212010-04-26 19:18:54 +0200264 int ret;
265
Harald Weltecca49632012-06-16 17:45:59 +0800266 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200267
Harald Welte144e0292010-05-13 11:45:07 +0200268 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200269 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
270 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200271
Harald Welteb3ee2652010-05-19 14:38:50 +0200272 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200273 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200274 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200275 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200276 case GPRS_NS_LL_FR_GRE:
277 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
278 break;
Harald Weltef030b212010-04-26 19:18:54 +0200279 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200280 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200281 msgb_free(msg);
282 ret = -EIO;
283 break;
284 }
285 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800286}
287
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200288static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800289{
Harald Welteba4c6662010-05-19 15:38:10 +0200290 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800291 struct gprs_ns_hdr *nsh;
292
Harald Weltecca49632012-06-16 17:45:59 +0800293 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200294
Harald Welte9ba50052010-03-14 15:45:01 +0800295 if (!msg)
296 return -ENOMEM;
297
Harald Welte144e0292010-05-13 11:45:07 +0200298 msg->l2h = msgb_put(msg, sizeof(*nsh));
299 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800300
301 nsh->pdu_type = pdu_type;
302
Harald Welte24a655f2010-04-30 19:54:29 +0200303 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800304}
305
Harald Weltea9f23c82011-11-23 15:01:31 +0100306/*! \brief Transmit a NS-RESET on a given NSVC
307 * \param[in] nsvc NS-VC used for transmission
308 * \paam[in] cause Numeric NS cause value
309 */
Harald Welte834f26d2010-05-11 06:20:54 +0200310int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200311{
Harald Welteba4c6662010-05-19 15:38:10 +0200312 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200313 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200314 uint16_t nsvci = htons(nsvc->nsvci);
315 uint16_t nsei = htons(nsvc->nsei);
316
Harald Weltecca49632012-06-16 17:45:59 +0800317 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200318
Harald Welte69a4cf22010-05-03 20:55:10 +0200319 if (!msg)
320 return -ENOMEM;
321
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000322 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
323 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
324
Harald Welte144e0292010-05-13 11:45:07 +0200325 msg->l2h = msgb_put(msg, sizeof(*nsh));
326 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200327 nsh->pdu_type = NS_PDUT_RESET;
328
329 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
330 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
331 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
332
333 return gprs_ns_tx(nsvc, msg);
334
335}
336
Harald Weltea9f23c82011-11-23 15:01:31 +0100337/*! \brief Transmit a NS-STATUS on a given NSVC
338 * \param[in] nsvc NS-VC to be used for transmission
339 * \param[in] cause Numeric NS cause value
340 * \param[in] bvci BVCI to be reset within NSVC
341 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200342int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
343 uint16_t bvci, struct msgb *orig_msg)
344{
Harald Welteba4c6662010-05-19 15:38:10 +0200345 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200346 struct gprs_ns_hdr *nsh;
347 uint16_t nsvci = htons(nsvc->nsvci);
348
Harald Weltecca49632012-06-16 17:45:59 +0800349 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200350
Harald Weltec1402a62010-05-12 11:48:44 +0200351 bvci = htons(bvci);
352
353 if (!msg)
354 return -ENOMEM;
355
Harald Welte90af1942010-05-15 23:02:24 +0200356 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000357 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
358
Harald Welte144e0292010-05-13 11:45:07 +0200359 msg->l2h = msgb_put(msg, sizeof(*nsh));
360 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200361 nsh->pdu_type = NS_PDUT_STATUS;
362
363 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
364
365 /* Section 9.2.7.1: Static conditions for NS-VCI */
366 if (cause == NS_CAUSE_NSVC_BLOCKED ||
367 cause == NS_CAUSE_NSVC_UNKNOWN)
368 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
369
370 /* Section 9.2.7.2: Static conditions for NS PDU */
371 switch (cause) {
372 case NS_CAUSE_SEM_INCORR_PDU:
373 case NS_CAUSE_PDU_INCOMP_PSTATE:
374 case NS_CAUSE_PROTO_ERR_UNSPEC:
375 case NS_CAUSE_INVAL_ESSENT_IE:
376 case NS_CAUSE_MISSING_ESSENT_IE:
377 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
378 orig_msg->l2h);
379 break;
380 default:
381 break;
382 }
383
384 /* Section 9.2.7.3: Static conditions for BVCI */
385 if (cause == NS_CAUSE_BVCI_UNKNOWN)
386 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
387
388 return gprs_ns_tx(nsvc, msg);
389}
390
Harald Weltea9f23c82011-11-23 15:01:31 +0100391/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
392 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
393 * \param[in] cause Numeric NS Cause value
394 * \returns 0 in case of success
395 */
Harald Welte834f26d2010-05-11 06:20:54 +0200396int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
397{
Harald Welteba4c6662010-05-19 15:38:10 +0200398 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200399 struct gprs_ns_hdr *nsh;
400 uint16_t nsvci = htons(nsvc->nsvci);
401
Harald Weltecca49632012-06-16 17:45:59 +0800402 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200403
Harald Welte834f26d2010-05-11 06:20:54 +0200404 if (!msg)
405 return -ENOMEM;
406
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000407 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
408 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
409
Harald Welte834f26d2010-05-11 06:20:54 +0200410 /* be conservative and mark it as blocked even now! */
411 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200412 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200413
Harald Welte144e0292010-05-13 11:45:07 +0200414 msg->l2h = msgb_put(msg, sizeof(*nsh));
415 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200416 nsh->pdu_type = NS_PDUT_BLOCK;
417
418 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
419 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
420
421 return gprs_ns_tx(nsvc, msg);
422}
423
Harald Weltea9f23c82011-11-23 15:01:31 +0100424/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
425 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
426 * \returns 0 in case of success
427 */
Harald Welte834f26d2010-05-11 06:20:54 +0200428int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
429{
Harald Weltecca49632012-06-16 17:45:59 +0800430 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000431 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
432 nsvc->nsei, nsvc->nsvci);
433
Harald Welte834f26d2010-05-11 06:20:54 +0200434 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
435}
436
Harald Weltea9f23c82011-11-23 15:01:31 +0100437/*! \brief Transmit a NS-ALIVE on a given NS-VC
438 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
439 * \returns 0 in case of success
440 */
Harald Welteb983c312010-05-12 12:11:45 +0000441int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
442{
Harald Weltecca49632012-06-16 17:45:59 +0800443 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000444 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
445 nsvc->nsei, nsvc->nsvci);
446
447 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
448}
449
Harald Weltea9f23c82011-11-23 15:01:31 +0100450/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
451 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
452 * \returns 0 in case of success
453 */
Harald Welteb983c312010-05-12 12:11:45 +0000454int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
455{
Harald Weltecca49632012-06-16 17:45:59 +0800456 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000457 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
458 nsvc->nsei, nsvc->nsvci);
459
460 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
461}
462
Harald Weltefe4ab902010-05-12 17:19:53 +0000463static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
464 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
465 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
466 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200467};
468
Harald Welte7c24b9e2010-05-12 12:21:52 +0000469static const struct value_string timer_mode_strs[] = {
470 { NSVC_TIMER_TNS_RESET, "tns-reset" },
471 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
472 { NSVC_TIMER_TNS_TEST, "tns-test" },
473 { 0, NULL }
474};
475
Harald Welte69a4cf22010-05-03 20:55:10 +0200476static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
477{
Harald Weltefe4ab902010-05-12 17:19:53 +0000478 enum ns_timeout tout = timer_mode_tout[mode];
479 unsigned int seconds = nsvc->nsi->timeout[tout];
480
Harald Weltecca49632012-06-16 17:45:59 +0800481 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000482 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
483 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000484 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000485
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200486 if (osmo_timer_pending(&nsvc->timer))
487 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200488
489 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200490 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200491}
492
Harald Welte80405452010-05-03 20:16:13 +0200493static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800494{
495 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000496 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
497 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800498
Harald Weltecca49632012-06-16 17:45:59 +0800499 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000500 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
501 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000502 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000503
Harald Welte80405452010-05-03 20:16:13 +0200504 switch (nsvc->timer_mode) {
505 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800506 /* Tns-alive case: we expired without response ! */
507 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000508 if (nsvc->alive_retries >
509 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800510 /* mark as dead and blocked */
511 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200512 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
513 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200514 LOGP(DNS, LOGL_NOTICE,
515 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200516 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000517 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200518 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
519 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800520 return;
521 }
Harald Welteb983c312010-05-12 12:11:45 +0000522 /* Tns-test case: send NS-ALIVE PDU */
523 gprs_ns_tx_alive(nsvc);
524 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200525 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200526 break;
527 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800528 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000529 gprs_ns_tx_alive(nsvc);
530 /* start Tns-alive timer (transition into faster
531 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000532 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200533 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
534 break;
535 case NSVC_TIMER_TNS_RESET:
536 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200537 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200538 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200539 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200540 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200541 case _NSVC_TIMER_NR:
542 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800543 }
Harald Welte9ba50052010-03-14 15:45:01 +0800544}
545
546/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800547static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800548{
Harald Welteba4c6662010-05-19 15:38:10 +0200549 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800550 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200551 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800552
Harald Weltecca49632012-06-16 17:45:59 +0800553 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800554 if (!msg)
555 return -ENOMEM;
556
Harald Weltec5478482010-03-18 00:01:43 +0800557 nsvci = htons(nsvc->nsvci);
558 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800559
Harald Welte144e0292010-05-13 11:45:07 +0200560 msg->l2h = msgb_put(msg, sizeof(*nsh));
561 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800562
563 nsh->pdu_type = NS_PDUT_RESET_ACK;
564
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200565 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200566 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800567
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200568 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
569 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800570
Harald Welte24a655f2010-04-30 19:54:29 +0200571 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800572}
573
Harald Weltea9f23c82011-11-23 15:01:31 +0100574/*! \brief High-level function for transmitting a NS-UNITDATA messsage
575 * \param[in] nsi NS-instance on which we shall transmit
576 * \param[in] msg struct msgb to be trasnmitted
577 *
578 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
579 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
580 * header for the NS-UNITDATA message type and sends it off.
581 *
582 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
583 */
Harald Welte24a655f2010-04-30 19:54:29 +0200584int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800585{
Harald Welte24a655f2010-04-30 19:54:29 +0200586 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800587 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200588 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200589
Jacob Erlbeck69017152013-10-14 12:03:54 +0200590 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200591 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200592 int rc;
593 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
594 LOGP(DNS, LOGL_ERROR,
595 "All NS-VCs for NSEI %u are either dead or blocked!\n",
596 msgb_nsei(msg));
597 rc = -EBUSY;
598 } else {
599 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
600 "to NS-VC!\n", msgb_nsei(msg));
601 rc = -EINVAL;
602 }
603
Harald Weltef47df642010-08-09 21:15:40 +0800604 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200605 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200606 }
Harald Weltecca49632012-06-16 17:45:59 +0800607 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800608
Harald Welteec20ba42010-05-13 12:18:49 +0200609 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
610 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800611 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200612 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800613 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800614 return -EIO;
615 }
616
617 nsh->pdu_type = NS_PDUT_UNITDATA;
618 /* spare octet in data[0] */
619 nsh->data[1] = bvci >> 8;
620 nsh->data[2] = bvci & 0xff;
621
Harald Welte24a655f2010-04-30 19:54:29 +0200622 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800623}
624
625/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200626static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800627{
628 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200629 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800630
Harald Weltec1402a62010-05-12 11:48:44 +0200631 if (nsvc->state & NSE_S_BLOCKED)
632 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
633 0, msg);
634
Harald Welte9ba50052010-03-14 15:45:01 +0800635 /* spare octet in data[0] */
636 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200637 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200638 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800639
640 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200641 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800642}
643
644/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200645static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800646{
Harald Weltef030b212010-04-26 19:18:54 +0200647 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800648 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200649 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800650 int rc;
651
Harald Welte41337832010-05-16 00:24:26 +0200652 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800653
Harald Weltebd33f3d2010-05-30 17:19:38 +0200654 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
655 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200656 if (rc < 0) {
657 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
658 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
659 "Error during TLV Parse\n", nsvc->nsei);
660 return rc;
661 }
Harald Welte9ba50052010-03-14 15:45:01 +0800662
663 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200664 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800665 return -EINVAL;
666 }
667
668 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200669 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800670
671 return 0;
672}
673
674/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200675static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800676{
677 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800678 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200679 uint8_t cause;
680 uint16_t nsvci, nsei;
681 struct gprs_nsvc *other_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800682 int rc;
683
Harald Weltebd33f3d2010-05-30 17:19:38 +0200684 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
685 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200686 if (rc < 0) {
687 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200688 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200689 return rc;
690 }
Harald Welte9ba50052010-03-14 15:45:01 +0800691
692 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
693 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
694 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200695 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200696 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800697 return -EINVAL;
698 }
699
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200700 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
701 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
702 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Harald Welte9ba50052010-03-14 15:45:01 +0800703
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200704 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
705 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
706 nsei, nsvci, gprs_ns_cause_str(cause));
707
708 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
709 /* NS-VCI has changed */
710 other_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
711
712 if (other_nsvc) {
713 /* The NS-VCI is already used by this NS-VC */
714
715 struct gprs_nsvc *tmp_nsvc;
716 char *old_peer;
717
718 /* Exchange the NS-VC objects */
719 tmp_nsvc = *nsvc;
720 *nsvc = other_nsvc;
721 other_nsvc = tmp_nsvc;
722
723 /* Do logging */
724 old_peer = talloc_strdup(other_nsvc,
725 gprs_ns_ll_str(other_nsvc));
726 LOGP(DNS, LOGL_INFO,
727 "NS-VC changed link (NSVCI=%u) from %s to %s\n",
728 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
729
730 talloc_free(old_peer);
731
732 /* Do statistics */
733 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
734 }
735 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200736
Harald Weltec1402a62010-05-12 11:48:44 +0200737 /* Mark NS-VC as blocked and alive */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200738 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200739
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200740 if (other_nsvc) {
741 /* Check NSEI */
742 if ((*nsvc)->nsei != nsei) {
743 LOGP(DNS, LOGL_NOTICE,
744 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
745 nsvci, (*nsvc)->nsei, nsei);
746
747 /* Override old NSEI */
748 (*nsvc)->nsei = nsei;
749
750 /* Do statistics */
751 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
752 }
753
754 ns_osmo_signal_dispatch_replaced(*nsvc, other_nsvc);
755
756 /* Update the ll info fields */
757 gprs_ns_ll_copy(*nsvc, other_nsvc);
758 gprs_ns_ll_clear(other_nsvc);
759 } else {
760 (*nsvc)->nsei = nsei;
761 (*nsvc)->nsvci = nsvci;
762 (*nsvc)->nsvci_is_valid = 1;
763 }
Harald Welte9ba50052010-03-14 15:45:01 +0800764
Harald Welte834f26d2010-05-11 06:20:54 +0200765 /* inform interested parties about the fact that this NSVC
766 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200767 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200768
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200769 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200770
771 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200772 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
773 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200774
775 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800776}
777
Harald Welte834f26d2010-05-11 06:20:54 +0200778static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
779{
780 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
781 struct tlv_parsed tp;
782 uint8_t *cause;
783 int rc;
784
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200785 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200786
787 nsvc->state |= NSE_S_BLOCKED;
788
Harald Weltebd33f3d2010-05-30 17:19:38 +0200789 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
790 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200791 if (rc < 0) {
792 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
793 "Error during TLV Parse\n", nsvc->nsei);
794 return rc;
795 }
Harald Welte834f26d2010-05-11 06:20:54 +0200796
797 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
798 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200799 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200800 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200801 return -EINVAL;
802 }
803
804 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
805 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
806
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200807 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +0200808 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200809
810 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
811}
812
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200813int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
814 struct gprs_nsvc *fallback_nsvc,
815 struct gprs_nsvc **new_nsvc);
816
817int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200818 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200819
Harald Weltea9f23c82011-11-23 15:01:31 +0100820/*! \brief Receive incoming NS message from underlying transport layer
821 * \param nsi NS instance to which the data belongs
822 * \param[in] msg message buffer containing newly-received data
823 * \param[in] saddr socketaddr from which data was received
824 * \param[in] ll link-layer type in which data was received
825 * \returns 0 in case of success, < 0 in case of error
826 *
827 * This is the main entry point int othe NS imlementation where frames
828 * from the underlying transport (normally UDP) enter.
829 */
Harald Weltef030b212010-04-26 19:18:54 +0200830int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200831 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800832{
Harald Weltef030b212010-04-26 19:18:54 +0200833 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800834 int rc = 0;
835
Harald Weltef030b212010-04-26 19:18:54 +0200836 /* look up the NSVC based on source address */
837 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200838
Harald Weltef030b212010-04-26 19:18:54 +0200839 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200840 struct gprs_nsvc *fallback_nsvc;
841
842 fallback_nsvc = nsi->unknown_nsvc;
843 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
844 fallback_nsvc->ip.bts_addr = *saddr;
845 fallback_nsvc->ll = ll;
846
847 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
848
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200849 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +0200850 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200851
852 rc = 0;
853 }
854
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200855 if (nsvc)
856 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200857
858 return rc;
859}
860
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200861const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200862{
863 static char buf[80];
864 snprintf(buf, sizeof(buf), "%s:%u",
865 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
866 ntohs(nsvc->ip.bts_addr.sin_port));
867
868 return buf;
869}
870
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200871void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
872{
873 nsvc->ll = other->ll;
874
875 switch (nsvc->ll) {
876 case GPRS_NS_LL_UDP:
877 nsvc->ip = other->ip;
878 break;
879 case GPRS_NS_LL_FR_GRE:
880 nsvc->frgre = other->frgre;
881 break;
882 default:
883 break;
884 }
885}
886
887void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
888{
889 switch (nsvc->ll) {
890 case GPRS_NS_LL_UDP:
891 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
892 nsvc->ip.bts_addr.sin_port = 0;
893 break;
894 case GPRS_NS_LL_FR_GRE:
895 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
896 nsvc->frgre.bts_addr.sin_port = 0;
897 break;
898 default:
899 break;
900 }
901}
902
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200903/*! \brief Create/get NS-VC independently from underlying transport layer
904 * \param nsi NS instance to which the data belongs
905 * \param[in] msg message buffer containing newly-received data
906 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200907 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200908 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
909 * been created or found
910 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
911 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
912 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
913 * has been created and registered, and GPRS_NS_CS_FOUND if an
914 * existing NS-VC object has been found with the same NSEI.
915 *
916 * This contains the initial NS automaton state (NS-VC not yet attached).
917 */
918int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
919 struct gprs_nsvc *fallback_nsvc,
920 struct gprs_nsvc **new_nsvc)
921{
922 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
923 struct gprs_nsvc *existing_nsvc;
924
925 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200926 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200927 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200928
929 int rc;
930
931 if (nsh->pdu_type == NS_PDUT_STATUS) {
932 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
933 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200934 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200935 return GPRS_NS_CS_SKIPPED;
936 }
937
938 /* Only the RESET procedure creates a new NSVC */
939 if (nsh->pdu_type != NS_PDUT_RESET) {
940 /* Since we have no NSVC, we have to use a fake */
941 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
942 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
943 "from %s for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200944 nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200945 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
946 fallback_nsvc->state = NSE_S_ALIVE;
947
948 rc = gprs_ns_tx_status(fallback_nsvc,
949 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
950 if (rc < 0) {
951 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200952 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200953 return rc;
954 }
955 return GPRS_NS_CS_REJECTED;
956 }
957
958 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
959 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
960 if (rc < 0) {
961 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
962 "TLV Parse\n", rc);
963 return rc;
964 }
965 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
966 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
967 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
968 gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
969 msg);
970 return -EINVAL;
971 }
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200972 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200973 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200974 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200975 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200976 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200977 if (!existing_nsvc) {
978 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
979 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200980 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200981 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200982 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200983
984 return GPRS_NS_CS_CREATED;
985 }
986
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200987 /* Check NSEI */
988 if (existing_nsvc->nsei != nsei) {
989 LOGP(DNS, LOGL_NOTICE,
990 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
991 nsvci, existing_nsvc->nsei, nsei);
992
993 /* Override old NSEI */
994 existing_nsvc->nsei = nsei;
995
996 /* Do statistics */
997 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
998 }
999
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001000 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001001 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001002 return GPRS_NS_CS_FOUND;
1003}
1004
1005/*! \brief Process NS message independently from underlying transport layer
1006 * \param nsi NS instance to which the data belongs
1007 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001008 * \param[inout] nsvc refers to the virtual connection, may be modified when
1009 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001010 * \returns 0 in case of success, < 0 in case of error
1011 *
1012 * This contains the main NS automaton.
1013 */
1014int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001015 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001016{
1017 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1018 int rc = 0;
1019
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001020 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001021
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001022 log_set_context(GPRS_CTX_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001023
Harald Welte144e0292010-05-13 11:45:07 +02001024 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001025 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1026 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001027
Harald Welte9ba50052010-03-14 15:45:01 +08001028 switch (nsh->pdu_type) {
1029 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001030 /* If we're dead and blocked and suddenly receive a
1031 * NS-ALIVE out of the blue, we might have been re-started
1032 * and should send a NS-RESET to make sure everything recovers
1033 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001034 if ((*nsvc)->state == NSE_S_BLOCKED)
1035 rc = gprs_ns_tx_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
Harald Welte2bffac52010-05-12 15:55:23 +00001036 else
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001037 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001038 break;
1039 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +02001040 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001041 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1042 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001043 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001044 if ((*nsvc)->state & NSE_S_BLOCKED)
1045 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001046 }
Harald Welte9ba50052010-03-14 15:45:01 +08001047 break;
1048 case NS_PDUT_UNITDATA:
1049 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001050 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001051 break;
1052 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001053 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001054 break;
1055 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001056 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001057 break;
1058 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001059 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001060 /* mark NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001061 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
1062 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
1063 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1064 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +02001065 /* stop RESET timer */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001066 osmo_timer_del(&(*nsvc)->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +02001067 }
Harald Welte90af1942010-05-15 23:02:24 +02001068 /* Initiate TEST proc.: Send ALIVE and start timer */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001069 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
1070 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +08001071 break;
1072 case NS_PDUT_UNBLOCK:
1073 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001074 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
1075 (*nsvc)->state &= ~NSE_S_BLOCKED;
1076 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1077 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001078 break;
1079 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001080 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001081 /* mark NS-VC as unblocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001082 (*nsvc)->state = NSE_S_ALIVE;
1083 (*nsvc)->remote_state = NSE_S_ALIVE;
1084 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001085 break;
1086 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001087 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001088 break;
1089 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001090 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001091 /* mark remote NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001092 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001093 break;
1094 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001095 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001096 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001097 rc = -EINVAL;
1098 break;
1099 }
1100 return rc;
1101}
1102
Harald Weltea9f23c82011-11-23 15:01:31 +01001103/*! \brief Create a new GPRS NS instance
1104 * \param[in] cb Call-back function for incoming BSSGP data
1105 * \returns dynamically allocated gprs_ns_inst
1106 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001107struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001108{
Harald Welte4fcdd762012-06-16 16:40:42 +08001109 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001110
1111 nsi->cb = cb;
1112 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001113 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1114 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1115 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1116 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1117 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1118 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1119 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001120
Harald Welte60cc6ac2010-05-13 14:20:56 +02001121 /* Create the dummy NSVC that we use for sending
1122 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001123 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +02001124 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001125
Harald Welte3771d092010-04-30 20:26:32 +02001126 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001127}
1128
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001129void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001130{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001131 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001132
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001133 /* delete all NSVCs and clear their timers */
1134 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1135 gprs_nsvc_delete(nsvc);
1136
1137 /* close socket and unregister */
1138 if (nsi->nsip.fd.data) {
1139 close(nsi->nsip.fd.fd);
1140 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001141 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001142 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001143}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001144
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001145/*! \brief Destroy an entire NS instance
1146 * \param nsi gprs_ns_inst that is to be destroyed
1147 *
1148 * This function releases all resources associated with the
1149 * NS-instance.
1150 */
1151void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1152{
1153 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001154 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001155 talloc_free(nsi);
1156}
1157
1158
1159/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1160 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1161
1162/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001163static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001164 struct sockaddr_in *saddr)
1165{
Harald Welteba4c6662010-05-19 15:38:10 +02001166 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001167 int ret = 0;
1168 socklen_t saddr_len = sizeof(*saddr);
1169
1170 if (!msg) {
1171 *error = -ENOMEM;
1172 return NULL;
1173 }
1174
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001175 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001176 (struct sockaddr *)saddr, &saddr_len);
1177 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001178 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001179 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001180 msgb_free(msg);
1181 *error = ret;
1182 return NULL;
1183 } else if (ret == 0) {
1184 msgb_free(msg);
1185 *error = ret;
1186 return NULL;
1187 }
1188
1189 msg->l2h = msg->data;
1190 msgb_put(msg, ret);
1191
1192 return msg;
1193}
1194
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001195static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001196{
1197 int error;
1198 struct sockaddr_in saddr;
1199 struct gprs_ns_inst *nsi = bfd->data;
1200 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1201
1202 if (!msg)
1203 return error;
1204
Harald Welteb3ee2652010-05-19 14:38:50 +02001205 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001206
1207 msgb_free(msg);
1208
1209 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001210}
1211
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001212static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001213{
1214 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1215 return -EIO;
1216}
1217
Harald Welteb3ee2652010-05-19 14:38:50 +02001218static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001219{
1220 int rc;
1221 struct gprs_ns_inst *nsi = nsvc->nsi;
1222 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1223
1224 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1225 (struct sockaddr *)daddr, sizeof(*daddr));
1226
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001227 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001228
1229 return rc;
1230}
1231
1232/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001233static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001234{
1235 int rc = 0;
1236
1237 if (what & BSC_FD_READ)
1238 rc = handle_nsip_read(bfd);
1239 if (what & BSC_FD_WRITE)
1240 rc = handle_nsip_write(bfd);
1241
1242 return rc;
1243}
1244
Harald Weltea9f23c82011-11-23 15:01:31 +01001245/*! \brief Create a listening socket for GPRS NS/UDP/IP
1246 * \param[in] nsi NS protocol instance to listen
1247 * \returns >=0 (fd) in case of success, negative in case of error
1248 *
1249 * A call to this function will create a UDP socket bound to the port
1250 * number and IP address specified in the NS protocol instance. The
1251 * file descriptor of the socket will be stored in nsi->nsip.fd.
1252 */
Harald Welte7fb05232010-05-19 15:09:09 +02001253int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001254{
Harald Welte73952e32012-06-16 14:59:56 +08001255 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001256 int ret;
1257
Harald Welte73952e32012-06-16 14:59:56 +08001258 in.s_addr = htonl(nsi->nsip.local_ip);
1259
1260 nsi->nsip.fd.cb = nsip_fd_cb;
1261 nsi->nsip.fd.data = nsi;
1262 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1263 IPPROTO_UDP, inet_ntoa(in),
1264 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001265 if (ret < 0)
1266 return ret;
1267
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001268 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1269 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1270 if (ret < 0)
1271 LOGP(DNS, LOGL_ERROR,
1272 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1273 nsi->nsip.dscp, ret, errno);
1274
Harald Weltef030b212010-04-26 19:18:54 +02001275
1276 return ret;
1277}
Harald Welte3771d092010-04-30 20:26:32 +02001278
Harald Weltea9f23c82011-11-23 15:01:31 +01001279/*! \brief Initiate a RESET procedure
1280 * \param[in] nsvc NS-VC in which to start the procedure
1281 * \param[in] cause Numeric NS cause value
1282 *
1283 * This is a high-level function initiating a NS-RESET procedure. It
1284 * will not only send a NS-RESET, but also set the state to BLOCKED and
1285 * start the Tns-reset timer.
1286 */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001287void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001288{
Harald Welteb1020d52010-05-25 22:17:30 +02001289 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1290 nsvc->nsei);
1291
Harald Welte731d1fc2010-05-14 11:53:08 +00001292 /* Mark NS-VC locally as blocked and dead */
1293 nsvc->state = NSE_S_BLOCKED;
1294 /* Send NS-RESET PDU */
1295 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
1296 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1297 nsvc->nsei);
1298 }
1299 /* Start Tns-reset */
1300 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +00001301}
1302
Harald Weltea9f23c82011-11-23 15:01:31 +01001303/*! \brief Establish a NS connection (from the BSS) to the SGSN
1304 * \param nsi NS-instance
1305 * \param[in] dest Destination IP/Port
1306 * \param[in] nsei NSEI of the to-be-established NS-VC
1307 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1308 * \returns struct gprs_nsvc representing the new NS-VC
1309 *
1310 * This function will establish a single NS/UDP/IP connection in uplink
1311 * (BSS to SGSN) direction.
1312 */
Harald Weltef5430362012-06-17 12:25:53 +08001313struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001314 struct sockaddr_in *dest, uint16_t nsei,
1315 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001316{
1317 struct gprs_nsvc *nsvc;
1318
1319 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001320 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001321 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001322 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001323 nsvc->nsei = nsei;
1324 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +02001325 nsvc->remote_end_is_sgsn = 1;
1326
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001327 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1328 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001329}
Harald Weltea9f23c82011-11-23 15:01:31 +01001330
Harald Weltecca49632012-06-16 17:45:59 +08001331void gprs_ns_set_log_ss(int ss)
1332{
1333 DNS = ss;
1334}
1335
Harald Weltea9f23c82011-11-23 15:01:31 +01001336/*! }@ */