blob: afc62497eb79342b63816b70a9cf382789779837 [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 Welte7fa89c22014-10-26 20:33:09 +01009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
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 Welte7fa89c22014-10-26 20:33:09 +010016 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080017 *
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010019 * 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>
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +020078#include <osmocom/core/stat_item.h>
Harald Welte73952e32012-06-16 14:59:56 +080079#include <osmocom/core/socket.h>
Harald Welte4fcdd762012-06-16 16:40:42 +080080#include <osmocom/core/signal.h>
Harald Welte73952e32012-06-16 14:59:56 +080081#include <osmocom/gprs/gprs_ns.h>
82#include <osmocom/gprs/gprs_bssgp.h>
83#include <osmocom/gprs/gprs_ns_frgre.h>
84
Harald Weltecca49632012-06-16 17:45:59 +080085#include "common_vty.h"
Harald Welte9ba50052010-03-14 15:45:01 +080086
Harald Welte9ba50052010-03-14 15:45:01 +080087static const struct tlv_definition ns_att_tlvdef = {
88 .def = {
89 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
90 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
91 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
92 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
93 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
94 },
95};
96
Harald Weltec51c23c2010-05-13 12:55:20 +020097enum ns_ctr {
98 NS_CTR_PKTS_IN,
99 NS_CTR_PKTS_OUT,
100 NS_CTR_BYTES_IN,
101 NS_CTR_BYTES_OUT,
102 NS_CTR_BLOCKED,
103 NS_CTR_DEAD,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200104 NS_CTR_REPLACED,
105 NS_CTR_NSEI_CHG,
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200106 NS_CTR_INV_VCI,
107 NS_CTR_INV_NSEI,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200108 NS_CTR_LOST_ALIVE,
109 NS_CTR_LOST_RESET,
Harald Weltec51c23c2010-05-13 12:55:20 +0200110};
111
Harald Welte144e0292010-05-13 11:45:07 +0200112static const struct rate_ctr_desc nsvc_ctr_description[] = {
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200113 { "packets.in", "Packets at NS Level ( In)" },
114 { "packets.out","Packets at NS Level (Out)" },
115 { "bytes.in", "Bytes at NS Level ( In)" },
116 { "bytes.out", "Bytes at NS Level (Out)" },
117 { "blocked", "NS-VC Block count " },
118 { "dead", "NS-VC gone dead count " },
119 { "replaced", "NS-VC replaced other count" },
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200120 { "nsei-chg", "NS-VC changed NSEI count " },
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200121 { "inv-nsvci", "NS-VCI was invalid count " },
122 { "inv-nsei", "NSEI was invalid count " },
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200123 { "lost.alive", "ALIVE ACK missing count " },
124 { "lost.reset", "RESET ACK missing count " },
Harald Welte144e0292010-05-13 11:45:07 +0200125};
126
127static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200128 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200129 .group_description = "NSVC Peer Statistics",
130 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
131 .ctr_desc = nsvc_ctr_description,
132};
133
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200134enum ns_stat {
135 NS_STAT_ALIVE_DELAY,
136};
137
138static const struct stat_item_desc nsvc_stat_description[] = {
139 { "alive.delay", "ALIVE reponse time ", "ms", 16, 0 },
140};
141
142static const struct stat_item_group_desc nsvc_statg_desc = {
143 .group_name_prefix = "ns.nsvc",
144 .group_description = "NSVC Peer Statistics",
145 .num_items = ARRAY_SIZE(nsvc_stat_description),
146 .item_desc = nsvc_stat_description,
147};
148
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200149#define CHECK_TX_RC(rc, nsvc) \
150 if (rc < 0) \
151 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", \
152 rc, gprs_ns_ll_str(nsvc));
153
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200154struct msgb *gprs_ns_msgb_alloc(void)
155{
156 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
157 "GPRS/NS");
158 if (!msg) {
159 LOGP(DNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
160 NS_ALLOC_SIZE);
161 }
162 return msg;
163}
164
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200165
Harald Weltea9f23c82011-11-23 15:01:31 +0100166/*! \brief Lookup struct gprs_nsvc based on NSVCI
167 * \param[in] nsi NS instance in which to search
168 * \param[in] nsvci NSVCI to be searched
169 * \returns gprs_nsvc of respective NSVCI
170 */
Harald Weltef5430362012-06-17 12:25:53 +0800171struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200172{
173 struct gprs_nsvc *nsvc;
174 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
175 if (nsvc->nsvci == nsvci)
176 return nsvc;
177 }
178 return NULL;
179}
180
Harald Weltea9f23c82011-11-23 15:01:31 +0100181/*! \brief Lookup struct gprs_nsvc based on NSEI
182 * \param[in] nsi NS instance in which to search
183 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200184 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100185 */
Harald Weltef5430362012-06-17 12:25:53 +0800186struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200187{
188 struct gprs_nsvc *nsvc;
189 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
190 if (nsvc->nsei == nsei)
191 return nsvc;
192 }
193 return NULL;
194}
195
Jacob Erlbeck69017152013-10-14 12:03:54 +0200196static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
197 uint16_t nsei)
198{
199 struct gprs_nsvc *nsvc;
200 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
201 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200202 if (!(nsvc->state & NSE_S_BLOCKED) &&
203 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200204 return nsvc;
205 }
206 }
207 return NULL;
208}
209
Harald Weltef030b212010-04-26 19:18:54 +0200210/* Lookup struct gprs_nsvc based on remote peer socket addr */
211static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
212 struct sockaddr_in *sin)
213{
214 struct gprs_nsvc *nsvc;
215 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000216 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
217 sin->sin_addr.s_addr &&
218 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200219 return nsvc;
220 }
221 return NULL;
222}
223
Harald Welte69a4cf22010-05-03 20:55:10 +0200224static void gprs_ns_timer_cb(void *data);
225
Harald Weltef5430362012-06-17 12:25:53 +0800226struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200227{
228 struct gprs_nsvc *nsvc;
229
Harald Welteb1020d52010-05-25 22:17:30 +0200230 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
231
Harald Weltef030b212010-04-26 19:18:54 +0200232 nsvc = talloc_zero(nsi, struct gprs_nsvc);
233 nsvc->nsvci = nsvci;
Jacob Erlbeck9b591b72013-11-11 09:43:05 +0100234 nsvc->nsvci_is_valid = 1;
Harald Weltef030b212010-04-26 19:18:54 +0200235 /* before RESET procedure: BLOCKED and DEAD */
236 nsvc->state = NSE_S_BLOCKED;
237 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200238 nsvc->timer.cb = gprs_ns_timer_cb;
239 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200240 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200241 nsvc->statg = stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200242
Harald Weltef030b212010-04-26 19:18:54 +0200243 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
244
245 return nsvc;
246}
Harald Welte9ba50052010-03-14 15:45:01 +0800247
Harald Weltea9f23c82011-11-23 15:01:31 +0100248/*! \brief Delete given NS-VC
249 * \param[in] nsvc gprs_nsvc to be deleted
250 */
Harald Weltef5430362012-06-17 12:25:53 +0800251void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000252{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200253 if (osmo_timer_pending(&nsvc->timer))
254 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000255 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200256 rate_ctr_group_free(nsvc->ctrg);
Harald Welte2bffac52010-05-12 15:55:23 +0000257 talloc_free(nsvc);
258}
259
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200260static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200261 uint8_t cause)
262{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200263 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200264
265 nssd.nsvc = nsvc;
266 nssd.cause = cause;
267
Harald Welte4fcdd762012-06-16 16:40:42 +0800268 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200269}
270
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200271static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
272 struct msgb *msg,
273 uint8_t pdu_type, uint8_t ie_type)
274{
275 struct ns_signal_data nssd = {0};
276
277 nssd.nsvc = nsvc;
278 nssd.pdu_type = pdu_type;
279 nssd.ie_type = ie_type;
280 nssd.msg = msg;
281
282 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
283}
284
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200285static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
286{
287 struct ns_signal_data nssd = {0};
288
289 nssd.nsvc = nsvc;
290 nssd.old_nsvc = old_nsvc;
291
292 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
293}
294
Harald Welte9ba50052010-03-14 15:45:01 +0800295/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200296static const struct value_string ns_cause_str[] = {
297 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
298 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
299 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
300 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
301 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
302 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
303 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
304 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200305 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200306 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
307 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
308 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800309};
310
Harald Weltea9f23c82011-11-23 15:01:31 +0100311/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200312const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800313{
Harald Welte2577d412010-05-02 09:37:45 +0200314 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800315}
316
Harald Welte24a655f2010-04-30 19:54:29 +0200317static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200318extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200319
Harald Welte24a655f2010-04-30 19:54:29 +0200320static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800321{
Harald Weltef030b212010-04-26 19:18:54 +0200322 int ret;
323
Harald Weltecca49632012-06-16 17:45:59 +0800324 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200325
Harald Welte144e0292010-05-13 11:45:07 +0200326 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200327 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
328 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200329
Harald Welteb3ee2652010-05-19 14:38:50 +0200330 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200331 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200332 ret = nsip_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200333 if (ret < 0)
334 LOGP(DNS, LOGL_INFO,
335 "failed to send NS message via UDP: %s\n",
336 strerror(-ret));
Harald Weltef030b212010-04-26 19:18:54 +0200337 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200338 case GPRS_NS_LL_FR_GRE:
339 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200340 if (ret < 0)
341 LOGP(DNS, LOGL_INFO,
342 "failed to send NS message via FR/GRE: %s\n",
343 strerror(-ret));
Harald Welteb3ee2652010-05-19 14:38:50 +0200344 break;
Harald Weltef030b212010-04-26 19:18:54 +0200345 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200346 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200347 msgb_free(msg);
348 ret = -EIO;
349 break;
350 }
351 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800352}
353
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200354static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800355{
Harald Welteba4c6662010-05-19 15:38:10 +0200356 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800357 struct gprs_ns_hdr *nsh;
358
Harald Weltecca49632012-06-16 17:45:59 +0800359 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200360
Harald Welte9ba50052010-03-14 15:45:01 +0800361 if (!msg)
362 return -ENOMEM;
363
Harald Welte144e0292010-05-13 11:45:07 +0200364 msg->l2h = msgb_put(msg, sizeof(*nsh));
365 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800366
367 nsh->pdu_type = pdu_type;
368
Harald Welte24a655f2010-04-30 19:54:29 +0200369 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800370}
371
Harald Weltea9f23c82011-11-23 15:01:31 +0100372/*! \brief Transmit a NS-RESET on a given NSVC
373 * \param[in] nsvc NS-VC used for transmission
374 * \paam[in] cause Numeric NS cause value
375 */
Harald Welte834f26d2010-05-11 06:20:54 +0200376int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200377{
Harald Welteba4c6662010-05-19 15:38:10 +0200378 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200379 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200380 uint16_t nsvci = htons(nsvc->nsvci);
381 uint16_t nsei = htons(nsvc->nsei);
382
Harald Weltecca49632012-06-16 17:45:59 +0800383 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200384
Harald Welte69a4cf22010-05-03 20:55:10 +0200385 if (!msg)
386 return -ENOMEM;
387
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000388 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
389 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
390
Harald Welte144e0292010-05-13 11:45:07 +0200391 msg->l2h = msgb_put(msg, sizeof(*nsh));
392 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200393 nsh->pdu_type = NS_PDUT_RESET;
394
395 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
396 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
397 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
398
399 return gprs_ns_tx(nsvc, msg);
400
401}
402
Harald Weltea9f23c82011-11-23 15:01:31 +0100403/*! \brief Transmit a NS-STATUS on a given NSVC
404 * \param[in] nsvc NS-VC to be used for transmission
405 * \param[in] cause Numeric NS cause value
406 * \param[in] bvci BVCI to be reset within NSVC
407 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200408int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
409 uint16_t bvci, struct msgb *orig_msg)
410{
Harald Welteba4c6662010-05-19 15:38:10 +0200411 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200412 struct gprs_ns_hdr *nsh;
413 uint16_t nsvci = htons(nsvc->nsvci);
414
Harald Weltecca49632012-06-16 17:45:59 +0800415 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200416
Harald Weltec1402a62010-05-12 11:48:44 +0200417 bvci = htons(bvci);
418
419 if (!msg)
420 return -ENOMEM;
421
Harald Welte90af1942010-05-15 23:02:24 +0200422 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000423 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
424
Harald Welte144e0292010-05-13 11:45:07 +0200425 msg->l2h = msgb_put(msg, sizeof(*nsh));
426 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200427 nsh->pdu_type = NS_PDUT_STATUS;
428
429 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
430
431 /* Section 9.2.7.1: Static conditions for NS-VCI */
432 if (cause == NS_CAUSE_NSVC_BLOCKED ||
433 cause == NS_CAUSE_NSVC_UNKNOWN)
434 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
435
436 /* Section 9.2.7.2: Static conditions for NS PDU */
437 switch (cause) {
438 case NS_CAUSE_SEM_INCORR_PDU:
439 case NS_CAUSE_PDU_INCOMP_PSTATE:
440 case NS_CAUSE_PROTO_ERR_UNSPEC:
441 case NS_CAUSE_INVAL_ESSENT_IE:
442 case NS_CAUSE_MISSING_ESSENT_IE:
443 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
444 orig_msg->l2h);
445 break;
446 default:
447 break;
448 }
449
450 /* Section 9.2.7.3: Static conditions for BVCI */
451 if (cause == NS_CAUSE_BVCI_UNKNOWN)
452 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
453
454 return gprs_ns_tx(nsvc, msg);
455}
456
Harald Weltea9f23c82011-11-23 15:01:31 +0100457/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
458 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
459 * \param[in] cause Numeric NS Cause value
460 * \returns 0 in case of success
461 */
Harald Welte834f26d2010-05-11 06:20:54 +0200462int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
463{
Harald Welteba4c6662010-05-19 15:38:10 +0200464 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200465 struct gprs_ns_hdr *nsh;
466 uint16_t nsvci = htons(nsvc->nsvci);
467
Harald Weltecca49632012-06-16 17:45:59 +0800468 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200469
Harald Welte834f26d2010-05-11 06:20:54 +0200470 if (!msg)
471 return -ENOMEM;
472
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000473 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
474 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
475
Harald Welte834f26d2010-05-11 06:20:54 +0200476 /* be conservative and mark it as blocked even now! */
477 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200478 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200479
Harald Welte144e0292010-05-13 11:45:07 +0200480 msg->l2h = msgb_put(msg, sizeof(*nsh));
481 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200482 nsh->pdu_type = NS_PDUT_BLOCK;
483
484 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
485 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
486
487 return gprs_ns_tx(nsvc, msg);
488}
489
Harald Weltea9f23c82011-11-23 15:01:31 +0100490/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
491 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
492 * \returns 0 in case of success
493 */
Harald Welte834f26d2010-05-11 06:20:54 +0200494int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
495{
Harald Weltecca49632012-06-16 17:45:59 +0800496 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000497 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
498 nsvc->nsei, nsvc->nsvci);
499
Harald Welte834f26d2010-05-11 06:20:54 +0200500 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
501}
502
Harald Weltea9f23c82011-11-23 15:01:31 +0100503/*! \brief Transmit a NS-ALIVE on a given NS-VC
504 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
505 * \returns 0 in case of success
506 */
Harald Welteb983c312010-05-12 12:11:45 +0000507int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
508{
Harald Weltecca49632012-06-16 17:45:59 +0800509 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000510 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
511 nsvc->nsei, nsvc->nsvci);
512
513 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
514}
515
Harald Weltea9f23c82011-11-23 15:01:31 +0100516/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
517 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
518 * \returns 0 in case of success
519 */
Harald Welteb983c312010-05-12 12:11:45 +0000520int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
521{
Harald Weltecca49632012-06-16 17:45:59 +0800522 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000523 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
524 nsvc->nsei, nsvc->nsvci);
525
526 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
527}
528
Harald Weltefe4ab902010-05-12 17:19:53 +0000529static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
530 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
531 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
532 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200533};
534
Harald Welte7c24b9e2010-05-12 12:21:52 +0000535static const struct value_string timer_mode_strs[] = {
536 { NSVC_TIMER_TNS_RESET, "tns-reset" },
537 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
538 { NSVC_TIMER_TNS_TEST, "tns-test" },
539 { 0, NULL }
540};
541
Harald Welte69a4cf22010-05-03 20:55:10 +0200542static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
543{
Harald Weltefe4ab902010-05-12 17:19:53 +0000544 enum ns_timeout tout = timer_mode_tout[mode];
545 unsigned int seconds = nsvc->nsi->timeout[tout];
546
Harald Weltecca49632012-06-16 17:45:59 +0800547 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000548 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
549 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000550 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000551
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200552 if (osmo_timer_pending(&nsvc->timer))
553 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200554
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200555 gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200556 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200557 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200558}
559
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200560static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
561{
562 struct timeval now, elapsed;
563 gettimeofday(&now, NULL);
564 timersub(&now, &nsvc->timer_started, &elapsed);
565
566 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
567}
568
Harald Welte80405452010-05-03 20:16:13 +0200569static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800570{
571 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000572 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
573 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800574
Harald Weltecca49632012-06-16 17:45:59 +0800575 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000576 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
577 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000578 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000579
Harald Welte80405452010-05-03 20:16:13 +0200580 switch (nsvc->timer_mode) {
581 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800582 /* Tns-alive case: we expired without response ! */
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200583 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Harald Welte9ba50052010-03-14 15:45:01 +0800584 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000585 if (nsvc->alive_retries >
586 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800587 /* mark as dead and blocked */
588 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200589 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
590 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200591 LOGP(DNS, LOGL_NOTICE,
592 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200593 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000594 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200595 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
596 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800597 return;
598 }
Harald Welteb983c312010-05-12 12:11:45 +0000599 /* Tns-test case: send NS-ALIVE PDU */
600 gprs_ns_tx_alive(nsvc);
601 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200602 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200603 break;
604 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800605 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000606 gprs_ns_tx_alive(nsvc);
607 /* start Tns-alive timer (transition into faster
608 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000609 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200610 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
611 break;
612 case NSVC_TIMER_TNS_RESET:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200613 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Harald Welte69a4cf22010-05-03 20:55:10 +0200614 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200615 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200616 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200617 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200618 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200619 case _NSVC_TIMER_NR:
620 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800621 }
Harald Welte9ba50052010-03-14 15:45:01 +0800622}
623
624/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800625static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800626{
Harald Welteba4c6662010-05-19 15:38:10 +0200627 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800628 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200629 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800630
Harald Weltecca49632012-06-16 17:45:59 +0800631 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800632 if (!msg)
633 return -ENOMEM;
634
Harald Weltec5478482010-03-18 00:01:43 +0800635 nsvci = htons(nsvc->nsvci);
636 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800637
Harald Welte144e0292010-05-13 11:45:07 +0200638 msg->l2h = msgb_put(msg, sizeof(*nsh));
639 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800640
641 nsh->pdu_type = NS_PDUT_RESET_ACK;
642
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200643 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200644 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800645
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200646 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
647 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800648
Harald Welte24a655f2010-04-30 19:54:29 +0200649 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800650}
651
Harald Weltea9f23c82011-11-23 15:01:31 +0100652/*! \brief High-level function for transmitting a NS-UNITDATA messsage
653 * \param[in] nsi NS-instance on which we shall transmit
654 * \param[in] msg struct msgb to be trasnmitted
655 *
656 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
657 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
658 * header for the NS-UNITDATA message type and sends it off.
659 *
660 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
661 */
Harald Welte24a655f2010-04-30 19:54:29 +0200662int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800663{
Harald Welte24a655f2010-04-30 19:54:29 +0200664 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800665 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200666 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200667
Jacob Erlbeck69017152013-10-14 12:03:54 +0200668 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200669 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200670 int rc;
671 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
672 LOGP(DNS, LOGL_ERROR,
673 "All NS-VCs for NSEI %u are either dead or blocked!\n",
674 msgb_nsei(msg));
675 rc = -EBUSY;
676 } else {
677 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
678 "to NS-VC!\n", msgb_nsei(msg));
679 rc = -EINVAL;
680 }
681
Harald Weltef47df642010-08-09 21:15:40 +0800682 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200683 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200684 }
Harald Weltecca49632012-06-16 17:45:59 +0800685 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800686
Harald Welteec20ba42010-05-13 12:18:49 +0200687 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
688 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800689 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200690 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800691 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800692 return -EIO;
693 }
694
695 nsh->pdu_type = NS_PDUT_UNITDATA;
696 /* spare octet in data[0] */
697 nsh->data[1] = bvci >> 8;
698 nsh->data[2] = bvci & 0xff;
699
Harald Welte24a655f2010-04-30 19:54:29 +0200700 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800701}
702
703/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200704static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800705{
706 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200707 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800708
Harald Weltec1402a62010-05-12 11:48:44 +0200709 if (nsvc->state & NSE_S_BLOCKED)
710 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
711 0, msg);
712
Harald Welte9ba50052010-03-14 15:45:01 +0800713 /* spare octet in data[0] */
714 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200715 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200716 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800717
718 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200719 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800720}
721
722/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200723static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800724{
Harald Weltef030b212010-04-26 19:18:54 +0200725 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800726 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200727 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800728 int rc;
729
Harald Welte41337832010-05-16 00:24:26 +0200730 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800731
Harald Weltebd33f3d2010-05-30 17:19:38 +0200732 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
733 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200734 if (rc < 0) {
735 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
736 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
737 "Error during TLV Parse\n", nsvc->nsei);
738 return rc;
739 }
Harald Welte9ba50052010-03-14 15:45:01 +0800740
741 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200742 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800743 return -EINVAL;
744 }
745
746 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200747 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800748
749 return 0;
750}
751
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200752/* Replace a nsvc object with another based on NSVCI.
753 * This function replaces looks for a NSVC with the given NSVCI and replaces it
754 * if possible and necessary. If replaced, the former value of *nsvc is
755 * returned in *old_nsvc.
756 * \return != 0 if *nsvc points to a matching NSVC.
757 */
758static int gprs_nsvc_replace_if_found(uint16_t nsvci,
759 struct gprs_nsvc **nsvc,
760 struct gprs_nsvc **old_nsvc)
761{
762 struct gprs_nsvc *matching_nsvc;
763
764 if ((*nsvc)->nsvci == nsvci) {
765 *old_nsvc = NULL;
766 return 1;
767 }
768
769 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
770
771 if (!matching_nsvc)
772 return 0;
773
774 /* The NS-VCI is already used by this NS-VC */
775
776 char *old_peer;
777
778 /* Exchange the NS-VC objects */
779 *old_nsvc = *nsvc;
780 *nsvc = matching_nsvc;
781
782 /* Do logging */
783 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
784 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
785 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
786
787 talloc_free(old_peer);
788
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200789 return 1;
790}
791
Harald Welte9ba50052010-03-14 15:45:01 +0800792/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200793static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800794{
795 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800796 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200797 uint8_t cause;
798 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200799 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800800 int rc;
801
Harald Weltebd33f3d2010-05-30 17:19:38 +0200802 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
803 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200804 if (rc < 0) {
805 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200806 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200807 return rc;
808 }
Harald Welte9ba50052010-03-14 15:45:01 +0800809
810 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
811 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
812 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200813 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200814 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800815 return -EINVAL;
816 }
817
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200818 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
819 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
820 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Harald Welte9ba50052010-03-14 15:45:01 +0800821
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200822 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
823 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
824 nsei, nsvci, gprs_ns_cause_str(cause));
825
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200826 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
827 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200828 /* The incoming RESET doesn't match the NSVCI. Send an
829 * appropriate RESET_ACK and ignore the RESET.
830 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
831 */
832 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
833 NS_PDUT_RESET,
834 NS_IE_VCI);
835 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
836 gprs_ns_tx_reset_ack(*nsvc);
837 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200838 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200839
840 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200841 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
842 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
843 "at %s\n",
844 nsvci, (*nsvc)->nsvci,
845 gprs_ns_ll_str(*nsvc));
846 orig_nsvc = *nsvc;
847 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200848 (*nsvc)->nsei = nsei;
849 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200850 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200851
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200852 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
853 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
854 /* The incoming RESET doesn't match the NSEI. Send an
855 * appropriate RESET_ACK and ignore the RESET.
856 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
857 */
858 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
859 NS_PDUT_RESET,
860 NS_IE_NSEI);
861 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200862 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200863 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200864 return 0;
865 }
866
867 /* NSEI has changed */
868 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
869 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200870 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200871
Harald Weltec1402a62010-05-12 11:48:44 +0200872 /* Mark NS-VC as blocked and alive */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200873 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200874
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200875 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200876 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200877 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200878
879 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200880 gprs_ns_ll_copy(*nsvc, orig_nsvc);
881 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200882 } else {
883 (*nsvc)->nsei = nsei;
884 (*nsvc)->nsvci = nsvci;
885 (*nsvc)->nsvci_is_valid = 1;
886 }
Harald Welte9ba50052010-03-14 15:45:01 +0800887
Harald Welte834f26d2010-05-11 06:20:54 +0200888 /* inform interested parties about the fact that this NSVC
889 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200890 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200891
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200892 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200893
894 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200895 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
896 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200897
898 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800899}
900
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200901static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
902{
903 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
904 struct tlv_parsed tp;
905 uint16_t nsvci, nsei;
906 struct gprs_nsvc *orig_nsvc = NULL;
907 int rc;
908
909 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
910 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
911 if (rc < 0) {
912 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
913 "Error during TLV Parse\n", (*nsvc)->nsei);
914 return rc;
915 }
916
917 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
918 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
919 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +0200920 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200921 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200922 return -EINVAL;
923 }
924
925 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
926 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
927
928 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
929 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
930 nsei, nsvci);
931
932 if (!((*nsvc)->state & NSE_S_RESET)) {
933 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
934 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
935 */
936 LOGP(DNS, LOGL_ERROR,
937 "NS RESET ACK Discarding unexpected message for "
938 "NS-VCI %d from SGSN NSEI=%d\n",
939 nsvci, nsei);
940 return 0;
941 }
942
943 if (!(*nsvc)->nsvci_is_valid) {
944 LOGP(DNS, LOGL_NOTICE,
945 "NS RESET ACK Uninitialised NS-VC (%u) for "
946 "NS-VCI %d, NSEI=%d from %s\n",
947 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
948 return -EINVAL;
949 }
950
951 if ((*nsvc)->nsvci != nsvci) {
952 /* NS-VCI has changed */
953
954 /* if !0, use another NSVC object that matches the NSVCI */
955 int use_other_nsvc;
956
957 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200958 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
959 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200960
961 if (use_other_nsvc)
962 /* Update *nsvc to point to the right NSVC object */
963 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
964 &orig_nsvc);
965
966 if (!use_other_nsvc) {
967 /* The incoming RESET_ACK doesn't match the NSVCI.
968 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
969 */
970 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
971 NS_PDUT_RESET_ACK,
972 NS_IE_VCI);
973 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
974 LOGP(DNS, LOGL_ERROR,
975 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
976 "from %s\n",
977 nsvci,
978 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
979 nsei, gprs_ns_ll_str(*nsvc));
980 return -EINVAL;
981 }
982
983 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200984 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200985 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
986
987 /* Update the ll info fields */
988 gprs_ns_ll_copy(*nsvc, orig_nsvc);
989 gprs_ns_ll_clear(orig_nsvc);
990 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200991 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
992 /* The incoming RESET_ACK doesn't match the NSEI.
993 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
994 */
995 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
996 NS_PDUT_RESET_ACK,
997 NS_IE_NSEI);
998 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
999 LOGP(DNS, LOGL_ERROR,
1000 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1001 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1002 return -EINVAL;
1003 }
1004
1005 /* NSEI has changed */
1006 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1007 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001008 }
1009
1010 /* Mark NS-VC as blocked and alive */
1011 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
1012 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
1013 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1014 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1015 /* stop RESET timer */
1016 osmo_timer_del(&(*nsvc)->timer);
1017 }
1018 /* Initiate TEST proc.: Send ALIVE and start timer */
1019 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
1020 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1021
1022 return rc;
1023}
1024
Harald Welte834f26d2010-05-11 06:20:54 +02001025static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1026{
1027 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1028 struct tlv_parsed tp;
1029 uint8_t *cause;
1030 int rc;
1031
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001032 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001033
1034 nsvc->state |= NSE_S_BLOCKED;
1035
Harald Weltebd33f3d2010-05-30 17:19:38 +02001036 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1037 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001038 if (rc < 0) {
1039 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1040 "Error during TLV Parse\n", nsvc->nsei);
1041 return rc;
1042 }
Harald Welte834f26d2010-05-11 06:20:54 +02001043
1044 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1045 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001046 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001047 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001048 return -EINVAL;
1049 }
1050
1051 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1052 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1053
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001054 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001055 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001056
1057 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
1058}
1059
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001060int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1061 struct gprs_nsvc *fallback_nsvc,
1062 struct gprs_nsvc **new_nsvc);
1063
1064int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001065 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001066
Harald Weltea9f23c82011-11-23 15:01:31 +01001067/*! \brief Receive incoming NS message from underlying transport layer
1068 * \param nsi NS instance to which the data belongs
1069 * \param[in] msg message buffer containing newly-received data
1070 * \param[in] saddr socketaddr from which data was received
1071 * \param[in] ll link-layer type in which data was received
1072 * \returns 0 in case of success, < 0 in case of error
1073 *
1074 * This is the main entry point int othe NS imlementation where frames
1075 * from the underlying transport (normally UDP) enter.
1076 */
Harald Weltef030b212010-04-26 19:18:54 +02001077int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001078 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001079{
Harald Weltef030b212010-04-26 19:18:54 +02001080 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001081 int rc = 0;
1082
Harald Weltef030b212010-04-26 19:18:54 +02001083 /* look up the NSVC based on source address */
1084 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001085
Harald Weltef030b212010-04-26 19:18:54 +02001086 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001087 struct gprs_nsvc *fallback_nsvc;
1088
1089 fallback_nsvc = nsi->unknown_nsvc;
1090 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1091 fallback_nsvc->ip.bts_addr = *saddr;
1092 fallback_nsvc->ll = ll;
1093
1094 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1095
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001096 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001097 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001098
1099 rc = 0;
1100 }
1101
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001102 if (nsvc)
1103 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001104
1105 return rc;
1106}
1107
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001108const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001109{
1110 static char buf[80];
1111 snprintf(buf, sizeof(buf), "%s:%u",
1112 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
1113 ntohs(nsvc->ip.bts_addr.sin_port));
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001114 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001115
1116 return buf;
1117}
1118
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001119void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1120{
1121 nsvc->ll = other->ll;
1122
1123 switch (nsvc->ll) {
1124 case GPRS_NS_LL_UDP:
1125 nsvc->ip = other->ip;
1126 break;
1127 case GPRS_NS_LL_FR_GRE:
1128 nsvc->frgre = other->frgre;
1129 break;
1130 default:
1131 break;
1132 }
1133}
1134
1135void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1136{
1137 switch (nsvc->ll) {
1138 case GPRS_NS_LL_UDP:
1139 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1140 nsvc->ip.bts_addr.sin_port = 0;
1141 break;
1142 case GPRS_NS_LL_FR_GRE:
1143 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1144 nsvc->frgre.bts_addr.sin_port = 0;
1145 break;
1146 default:
1147 break;
1148 }
1149}
1150
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001151/*! \brief Create/get NS-VC independently from underlying transport layer
1152 * \param nsi NS instance to which the data belongs
1153 * \param[in] msg message buffer containing newly-received data
1154 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001155 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001156 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1157 * been created or found
1158 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1159 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1160 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1161 * has been created and registered, and GPRS_NS_CS_FOUND if an
1162 * existing NS-VC object has been found with the same NSEI.
1163 *
1164 * This contains the initial NS automaton state (NS-VC not yet attached).
1165 */
1166int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1167 struct gprs_nsvc *fallback_nsvc,
1168 struct gprs_nsvc **new_nsvc)
1169{
1170 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1171 struct gprs_nsvc *existing_nsvc;
1172
1173 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001174 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001175 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001176
1177 int rc;
1178
1179 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001180 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001181 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1182 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001183 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001184 return GPRS_NS_CS_SKIPPED;
1185 }
1186
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001187 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1188 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1189 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1190 "for non-existing NS-VC\n",
1191 gprs_ns_ll_str(fallback_nsvc));
1192 return GPRS_NS_CS_SKIPPED;
1193 }
1194
1195 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1196 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1197 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1198 "for non-existing NS-VC\n",
1199 gprs_ns_ll_str(fallback_nsvc));
1200 return GPRS_NS_CS_SKIPPED;
1201 }
1202
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001203 /* Only the RESET procedure creates a new NSVC */
1204 if (nsh->pdu_type != NS_PDUT_RESET) {
1205 /* Since we have no NSVC, we have to use a fake */
1206 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1207 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
1208 "from %s for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001209 nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001210 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001211 fallback_nsvc->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001212 fallback_nsvc->state = NSE_S_ALIVE;
1213
1214 rc = gprs_ns_tx_status(fallback_nsvc,
1215 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1216 if (rc < 0) {
1217 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001218 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001219 return rc;
1220 }
1221 return GPRS_NS_CS_REJECTED;
1222 }
1223
1224 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1225 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1226 if (rc < 0) {
1227 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1228 "TLV Parse\n", rc);
1229 return rc;
1230 }
1231 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1232 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1233 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001234 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001235 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001236 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001237 return -EINVAL;
1238 }
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001239 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001240 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001241 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001242 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001243 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001244 if (!existing_nsvc) {
1245 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001246 (*new_nsvc)->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001247 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001248 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001249 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001250 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001251
1252 return GPRS_NS_CS_CREATED;
1253 }
1254
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001255 /* Check NSEI */
1256 if (existing_nsvc->nsei != nsei) {
1257 LOGP(DNS, LOGL_NOTICE,
1258 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1259 nsvci, existing_nsvc->nsei, nsei);
1260
1261 /* Override old NSEI */
1262 existing_nsvc->nsei = nsei;
1263
1264 /* Do statistics */
1265 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1266 }
1267
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001268 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001269 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001270 return GPRS_NS_CS_FOUND;
1271}
1272
1273/*! \brief Process NS message independently from underlying transport layer
1274 * \param nsi NS instance to which the data belongs
1275 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001276 * \param[inout] nsvc refers to the virtual connection, may be modified when
1277 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001278 * \returns 0 in case of success, < 0 in case of error
1279 *
1280 * This contains the main NS automaton.
1281 */
1282int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001283 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001284{
1285 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1286 int rc = 0;
1287
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001288 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001289
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001290 log_set_context(GPRS_CTX_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001291
Harald Welte144e0292010-05-13 11:45:07 +02001292 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001293 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1294 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001295
Harald Welte9ba50052010-03-14 15:45:01 +08001296 switch (nsh->pdu_type) {
1297 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001298 /* If we're dead and blocked and suddenly receive a
1299 * NS-ALIVE out of the blue, we might have been re-started
1300 * and should send a NS-RESET to make sure everything recovers
1301 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001302 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001303 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1304 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001305 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001306 break;
1307 case NS_PDUT_ALIVE_ACK:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001308 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
1309 stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
1310 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001311 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001312 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1313 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001314 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001315 if ((*nsvc)->state & NSE_S_BLOCKED)
1316 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001317 }
Harald Welte9ba50052010-03-14 15:45:01 +08001318 break;
1319 case NS_PDUT_UNITDATA:
1320 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001321 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001322 break;
1323 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001324 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001325 break;
1326 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001327 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001328 break;
1329 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001330 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001331 break;
1332 case NS_PDUT_UNBLOCK:
1333 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001334 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
1335 (*nsvc)->state &= ~NSE_S_BLOCKED;
1336 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1337 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001338 break;
1339 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001340 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001341 /* mark NS-VC as unblocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001342 (*nsvc)->state = NSE_S_ALIVE;
1343 (*nsvc)->remote_state = NSE_S_ALIVE;
1344 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001345 break;
1346 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001347 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001348 break;
1349 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001350 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001351 /* mark remote NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001352 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001353 break;
1354 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001355 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001356 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001357 rc = -EINVAL;
1358 break;
1359 }
1360 return rc;
1361}
1362
Harald Weltea9f23c82011-11-23 15:01:31 +01001363/*! \brief Create a new GPRS NS instance
1364 * \param[in] cb Call-back function for incoming BSSGP data
1365 * \returns dynamically allocated gprs_ns_inst
1366 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001367struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001368{
Harald Welte4fcdd762012-06-16 16:40:42 +08001369 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001370
1371 nsi->cb = cb;
1372 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001373 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1374 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1375 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1376 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1377 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1378 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1379 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001380
Harald Welte60cc6ac2010-05-13 14:20:56 +02001381 /* Create the dummy NSVC that we use for sending
1382 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001383 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001384 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001385 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001386 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001387
Harald Welte3771d092010-04-30 20:26:32 +02001388 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001389}
1390
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001391void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001392{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001393 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001394
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001395 gprs_nsvc_delete(nsi->unknown_nsvc);
1396
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001397 /* delete all NSVCs and clear their timers */
1398 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1399 gprs_nsvc_delete(nsvc);
1400
1401 /* close socket and unregister */
1402 if (nsi->nsip.fd.data) {
1403 close(nsi->nsip.fd.fd);
1404 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001405 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001406 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001407}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001408
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001409/*! \brief Destroy an entire NS instance
1410 * \param nsi gprs_ns_inst that is to be destroyed
1411 *
1412 * This function releases all resources associated with the
1413 * NS-instance.
1414 */
1415void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1416{
1417 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001418 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001419 talloc_free(nsi);
1420}
1421
1422
1423/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1424 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1425
1426/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001427static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001428 struct sockaddr_in *saddr)
1429{
Harald Welteba4c6662010-05-19 15:38:10 +02001430 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001431 int ret = 0;
1432 socklen_t saddr_len = sizeof(*saddr);
1433
1434 if (!msg) {
1435 *error = -ENOMEM;
1436 return NULL;
1437 }
1438
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001439 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001440 (struct sockaddr *)saddr, &saddr_len);
1441 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001442 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001443 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001444 msgb_free(msg);
1445 *error = ret;
1446 return NULL;
1447 } else if (ret == 0) {
1448 msgb_free(msg);
1449 *error = ret;
1450 return NULL;
1451 }
1452
1453 msg->l2h = msg->data;
1454 msgb_put(msg, ret);
1455
1456 return msg;
1457}
1458
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001459static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001460{
1461 int error;
1462 struct sockaddr_in saddr;
1463 struct gprs_ns_inst *nsi = bfd->data;
1464 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1465
1466 if (!msg)
1467 return error;
1468
Harald Welteb3ee2652010-05-19 14:38:50 +02001469 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001470
1471 msgb_free(msg);
1472
1473 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001474}
1475
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001476static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001477{
1478 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1479 return -EIO;
1480}
1481
Harald Welteb3ee2652010-05-19 14:38:50 +02001482static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001483{
1484 int rc;
1485 struct gprs_ns_inst *nsi = nsvc->nsi;
1486 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1487
1488 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1489 (struct sockaddr *)daddr, sizeof(*daddr));
1490
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001491 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001492
1493 return rc;
1494}
1495
1496/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001497static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001498{
1499 int rc = 0;
1500
1501 if (what & BSC_FD_READ)
1502 rc = handle_nsip_read(bfd);
1503 if (what & BSC_FD_WRITE)
1504 rc = handle_nsip_write(bfd);
1505
1506 return rc;
1507}
1508
Harald Weltea9f23c82011-11-23 15:01:31 +01001509/*! \brief Create a listening socket for GPRS NS/UDP/IP
1510 * \param[in] nsi NS protocol instance to listen
1511 * \returns >=0 (fd) in case of success, negative in case of error
1512 *
1513 * A call to this function will create a UDP socket bound to the port
1514 * number and IP address specified in the NS protocol instance. The
1515 * file descriptor of the socket will be stored in nsi->nsip.fd.
1516 */
Harald Welte7fb05232010-05-19 15:09:09 +02001517int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001518{
Harald Welte73952e32012-06-16 14:59:56 +08001519 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001520 int ret;
1521
Harald Welte73952e32012-06-16 14:59:56 +08001522 in.s_addr = htonl(nsi->nsip.local_ip);
1523
1524 nsi->nsip.fd.cb = nsip_fd_cb;
1525 nsi->nsip.fd.data = nsi;
1526 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1527 IPPROTO_UDP, inet_ntoa(in),
1528 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001529 if (ret < 0)
1530 return ret;
1531
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001532 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1533 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1534 if (ret < 0)
1535 LOGP(DNS, LOGL_ERROR,
1536 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1537 nsi->nsip.dscp, ret, errno);
1538
Harald Weltef030b212010-04-26 19:18:54 +02001539
1540 return ret;
1541}
Harald Welte3771d092010-04-30 20:26:32 +02001542
Harald Weltea9f23c82011-11-23 15:01:31 +01001543/*! \brief Initiate a RESET procedure
1544 * \param[in] nsvc NS-VC in which to start the procedure
1545 * \param[in] cause Numeric NS cause value
1546 *
1547 * This is a high-level function initiating a NS-RESET procedure. It
1548 * will not only send a NS-RESET, but also set the state to BLOCKED and
1549 * start the Tns-reset timer.
1550 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001551int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001552{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001553 int rc;
1554
Harald Welteb1020d52010-05-25 22:17:30 +02001555 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1556 nsvc->nsei);
1557
Harald Welte731d1fc2010-05-14 11:53:08 +00001558 /* Mark NS-VC locally as blocked and dead */
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001559 nsvc->state = NSE_S_BLOCKED | NSE_S_RESET;
1560
Harald Welte731d1fc2010-05-14 11:53:08 +00001561 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001562 rc = gprs_ns_tx_reset(nsvc, cause);
1563 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00001564 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1565 nsvc->nsei);
1566 }
1567 /* Start Tns-reset */
1568 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001569
1570 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00001571}
1572
Harald Weltea9f23c82011-11-23 15:01:31 +01001573/*! \brief Establish a NS connection (from the BSS) to the SGSN
1574 * \param nsi NS-instance
1575 * \param[in] dest Destination IP/Port
1576 * \param[in] nsei NSEI of the to-be-established NS-VC
1577 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1578 * \returns struct gprs_nsvc representing the new NS-VC
1579 *
1580 * This function will establish a single NS/UDP/IP connection in uplink
1581 * (BSS to SGSN) direction.
1582 */
Harald Weltef5430362012-06-17 12:25:53 +08001583struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001584 struct sockaddr_in *dest, uint16_t nsei,
1585 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001586{
1587 struct gprs_nsvc *nsvc;
1588
1589 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001590 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001591 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001592 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001593 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02001594 nsvc->remote_end_is_sgsn = 1;
1595
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001596 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1597 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001598}
Harald Weltea9f23c82011-11-23 15:01:31 +01001599
Harald Weltecca49632012-06-16 17:45:59 +08001600void gprs_ns_set_log_ss(int ss)
1601{
1602 DNS = ss;
1603}
1604
Harald Weltea9f23c82011-11-23 15:01:31 +01001605/*! }@ */