blob: d0be754d884e1fc40ec8ae133287c523c1c29c1b [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>
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010079#include <osmocom/core/stats.h>
Harald Welte73952e32012-06-16 14:59:56 +080080#include <osmocom/core/socket.h>
Harald Welte4fcdd762012-06-16 16:40:42 +080081#include <osmocom/core/signal.h>
Harald Welte73952e32012-06-16 14:59:56 +080082#include <osmocom/gprs/gprs_ns.h>
83#include <osmocom/gprs/gprs_bssgp.h>
84#include <osmocom/gprs/gprs_ns_frgre.h>
85
Harald Weltecca49632012-06-16 17:45:59 +080086#include "common_vty.h"
Harald Welte9ba50052010-03-14 15:45:01 +080087
Harald Welte9ba50052010-03-14 15:45:01 +080088static const struct tlv_definition ns_att_tlvdef = {
89 .def = {
90 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
91 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
92 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
93 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
94 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
95 },
96};
97
Harald Weltec51c23c2010-05-13 12:55:20 +020098enum ns_ctr {
99 NS_CTR_PKTS_IN,
100 NS_CTR_PKTS_OUT,
101 NS_CTR_BYTES_IN,
102 NS_CTR_BYTES_OUT,
103 NS_CTR_BLOCKED,
104 NS_CTR_DEAD,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200105 NS_CTR_REPLACED,
106 NS_CTR_NSEI_CHG,
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200107 NS_CTR_INV_VCI,
108 NS_CTR_INV_NSEI,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200109 NS_CTR_LOST_ALIVE,
110 NS_CTR_LOST_RESET,
Harald Weltec51c23c2010-05-13 12:55:20 +0200111};
112
Harald Welte144e0292010-05-13 11:45:07 +0200113static const struct rate_ctr_desc nsvc_ctr_description[] = {
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200114 { "packets.in", "Packets at NS Level ( In)" },
115 { "packets.out","Packets at NS Level (Out)" },
116 { "bytes.in", "Bytes at NS Level ( In)" },
117 { "bytes.out", "Bytes at NS Level (Out)" },
118 { "blocked", "NS-VC Block count " },
119 { "dead", "NS-VC gone dead count " },
120 { "replaced", "NS-VC replaced other count" },
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200121 { "nsei-chg", "NS-VC changed NSEI count " },
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200122 { "inv-nsvci", "NS-VCI was invalid count " },
123 { "inv-nsei", "NSEI was invalid count " },
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200124 { "lost.alive", "ALIVE ACK missing count " },
125 { "lost.reset", "RESET ACK missing count " },
Harald Welte144e0292010-05-13 11:45:07 +0200126};
127
128static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200129 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200130 .group_description = "NSVC Peer Statistics",
131 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
132 .ctr_desc = nsvc_ctr_description,
Jacob Erlbeckfed2a482015-11-03 16:18:56 +0100133 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte144e0292010-05-13 11:45:07 +0200134};
135
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200136enum ns_stat {
137 NS_STAT_ALIVE_DELAY,
138};
139
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100140static const struct osmo_stat_item_desc nsvc_stat_description[] = {
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200141 { "alive.delay", "ALIVE reponse time ", "ms", 16, 0 },
142};
143
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100144static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200145 .group_name_prefix = "ns.nsvc",
146 .group_description = "NSVC Peer Statistics",
147 .num_items = ARRAY_SIZE(nsvc_stat_description),
148 .item_desc = nsvc_stat_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100149 .class_id = OSMO_STATS_CLASS_PEER,
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200150};
151
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200152#define CHECK_TX_RC(rc, nsvc) \
153 if (rc < 0) \
154 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", \
155 rc, gprs_ns_ll_str(nsvc));
156
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200157struct msgb *gprs_ns_msgb_alloc(void)
158{
159 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
160 "GPRS/NS");
161 if (!msg) {
162 LOGP(DNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
163 NS_ALLOC_SIZE);
164 }
165 return msg;
166}
167
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200168
Harald Weltea9f23c82011-11-23 15:01:31 +0100169/*! \brief Lookup struct gprs_nsvc based on NSVCI
170 * \param[in] nsi NS instance in which to search
171 * \param[in] nsvci NSVCI to be searched
172 * \returns gprs_nsvc of respective NSVCI
173 */
Harald Weltef5430362012-06-17 12:25:53 +0800174struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200175{
176 struct gprs_nsvc *nsvc;
177 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
178 if (nsvc->nsvci == nsvci)
179 return nsvc;
180 }
181 return NULL;
182}
183
Harald Weltea9f23c82011-11-23 15:01:31 +0100184/*! \brief Lookup struct gprs_nsvc based on NSEI
185 * \param[in] nsi NS instance in which to search
186 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200187 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100188 */
Harald Weltef5430362012-06-17 12:25:53 +0800189struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200190{
191 struct gprs_nsvc *nsvc;
192 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
193 if (nsvc->nsei == nsei)
194 return nsvc;
195 }
196 return NULL;
197}
198
Jacob Erlbeck69017152013-10-14 12:03:54 +0200199static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
200 uint16_t nsei)
201{
202 struct gprs_nsvc *nsvc;
203 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
204 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200205 if (!(nsvc->state & NSE_S_BLOCKED) &&
206 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200207 return nsvc;
208 }
209 }
210 return NULL;
211}
212
Harald Weltef030b212010-04-26 19:18:54 +0200213/* Lookup struct gprs_nsvc based on remote peer socket addr */
214static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
215 struct sockaddr_in *sin)
216{
217 struct gprs_nsvc *nsvc;
218 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000219 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
220 sin->sin_addr.s_addr &&
221 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200222 return nsvc;
223 }
224 return NULL;
225}
226
Harald Welte69a4cf22010-05-03 20:55:10 +0200227static void gprs_ns_timer_cb(void *data);
228
Harald Weltef5430362012-06-17 12:25:53 +0800229struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200230{
231 struct gprs_nsvc *nsvc;
232
Harald Welteb1020d52010-05-25 22:17:30 +0200233 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
234
Harald Weltef030b212010-04-26 19:18:54 +0200235 nsvc = talloc_zero(nsi, struct gprs_nsvc);
236 nsvc->nsvci = nsvci;
Jacob Erlbeck9b591b72013-11-11 09:43:05 +0100237 nsvc->nsvci_is_valid = 1;
Harald Weltef030b212010-04-26 19:18:54 +0200238 /* before RESET procedure: BLOCKED and DEAD */
239 nsvc->state = NSE_S_BLOCKED;
240 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200241 nsvc->timer.cb = gprs_ns_timer_cb;
242 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200243 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100244 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200245
Harald Weltef030b212010-04-26 19:18:54 +0200246 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
247
248 return nsvc;
249}
Harald Welte9ba50052010-03-14 15:45:01 +0800250
Harald Weltea9f23c82011-11-23 15:01:31 +0100251/*! \brief Delete given NS-VC
252 * \param[in] nsvc gprs_nsvc to be deleted
253 */
Harald Weltef5430362012-06-17 12:25:53 +0800254void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000255{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200256 if (osmo_timer_pending(&nsvc->timer))
257 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000258 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200259 rate_ctr_group_free(nsvc->ctrg);
Harald Welte2bffac52010-05-12 15:55:23 +0000260 talloc_free(nsvc);
261}
262
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200263static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200264 uint8_t cause)
265{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200266 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200267
268 nssd.nsvc = nsvc;
269 nssd.cause = cause;
270
Harald Welte4fcdd762012-06-16 16:40:42 +0800271 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200272}
273
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200274static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
275 struct msgb *msg,
276 uint8_t pdu_type, uint8_t ie_type)
277{
278 struct ns_signal_data nssd = {0};
279
280 nssd.nsvc = nsvc;
281 nssd.pdu_type = pdu_type;
282 nssd.ie_type = ie_type;
283 nssd.msg = msg;
284
285 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
286}
287
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200288static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
289{
290 struct ns_signal_data nssd = {0};
291
292 nssd.nsvc = nsvc;
293 nssd.old_nsvc = old_nsvc;
294
295 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
296}
297
Harald Welte9ba50052010-03-14 15:45:01 +0800298/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200299static const struct value_string ns_cause_str[] = {
300 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
301 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
302 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
303 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
304 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
305 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
306 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
307 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200308 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200309 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
310 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
311 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800312};
313
Harald Weltea9f23c82011-11-23 15:01:31 +0100314/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200315const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800316{
Harald Welte2577d412010-05-02 09:37:45 +0200317 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800318}
319
Harald Welte24a655f2010-04-30 19:54:29 +0200320static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200321extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200322
Harald Welte24a655f2010-04-30 19:54:29 +0200323static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800324{
Harald Weltef030b212010-04-26 19:18:54 +0200325 int ret;
326
Harald Weltecca49632012-06-16 17:45:59 +0800327 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200328
Harald Welte144e0292010-05-13 11:45:07 +0200329 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200330 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
331 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200332
Harald Welteb3ee2652010-05-19 14:38:50 +0200333 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200334 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200335 ret = nsip_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200336 if (ret < 0)
337 LOGP(DNS, LOGL_INFO,
338 "failed to send NS message via UDP: %s\n",
339 strerror(-ret));
Harald Weltef030b212010-04-26 19:18:54 +0200340 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200341 case GPRS_NS_LL_FR_GRE:
342 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200343 if (ret < 0)
344 LOGP(DNS, LOGL_INFO,
345 "failed to send NS message via FR/GRE: %s\n",
346 strerror(-ret));
Harald Welteb3ee2652010-05-19 14:38:50 +0200347 break;
Harald Weltef030b212010-04-26 19:18:54 +0200348 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200349 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200350 msgb_free(msg);
351 ret = -EIO;
352 break;
353 }
354 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800355}
356
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200357static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800358{
Harald Welteba4c6662010-05-19 15:38:10 +0200359 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800360 struct gprs_ns_hdr *nsh;
361
Harald Weltecca49632012-06-16 17:45:59 +0800362 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200363
Harald Welte9ba50052010-03-14 15:45:01 +0800364 if (!msg)
365 return -ENOMEM;
366
Harald Welte144e0292010-05-13 11:45:07 +0200367 msg->l2h = msgb_put(msg, sizeof(*nsh));
368 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800369
370 nsh->pdu_type = pdu_type;
371
Harald Welte24a655f2010-04-30 19:54:29 +0200372 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800373}
374
Harald Weltea9f23c82011-11-23 15:01:31 +0100375/*! \brief Transmit a NS-RESET on a given NSVC
376 * \param[in] nsvc NS-VC used for transmission
377 * \paam[in] cause Numeric NS cause value
378 */
Harald Welte834f26d2010-05-11 06:20:54 +0200379int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200380{
Harald Welteba4c6662010-05-19 15:38:10 +0200381 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200382 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200383 uint16_t nsvci = htons(nsvc->nsvci);
384 uint16_t nsei = htons(nsvc->nsei);
385
Harald Weltecca49632012-06-16 17:45:59 +0800386 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200387
Harald Welte69a4cf22010-05-03 20:55:10 +0200388 if (!msg)
389 return -ENOMEM;
390
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000391 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
392 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
393
Harald Welte144e0292010-05-13 11:45:07 +0200394 msg->l2h = msgb_put(msg, sizeof(*nsh));
395 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200396 nsh->pdu_type = NS_PDUT_RESET;
397
398 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
399 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
400 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
401
402 return gprs_ns_tx(nsvc, msg);
403
404}
405
Harald Weltea9f23c82011-11-23 15:01:31 +0100406/*! \brief Transmit a NS-STATUS on a given NSVC
407 * \param[in] nsvc NS-VC to be used for transmission
408 * \param[in] cause Numeric NS cause value
409 * \param[in] bvci BVCI to be reset within NSVC
410 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200411int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
412 uint16_t bvci, struct msgb *orig_msg)
413{
Harald Welteba4c6662010-05-19 15:38:10 +0200414 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200415 struct gprs_ns_hdr *nsh;
416 uint16_t nsvci = htons(nsvc->nsvci);
417
Harald Weltecca49632012-06-16 17:45:59 +0800418 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200419
Harald Weltec1402a62010-05-12 11:48:44 +0200420 bvci = htons(bvci);
421
422 if (!msg)
423 return -ENOMEM;
424
Harald Welte90af1942010-05-15 23:02:24 +0200425 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000426 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
427
Harald Welte144e0292010-05-13 11:45:07 +0200428 msg->l2h = msgb_put(msg, sizeof(*nsh));
429 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200430 nsh->pdu_type = NS_PDUT_STATUS;
431
432 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
433
434 /* Section 9.2.7.1: Static conditions for NS-VCI */
435 if (cause == NS_CAUSE_NSVC_BLOCKED ||
436 cause == NS_CAUSE_NSVC_UNKNOWN)
437 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
438
439 /* Section 9.2.7.2: Static conditions for NS PDU */
440 switch (cause) {
441 case NS_CAUSE_SEM_INCORR_PDU:
442 case NS_CAUSE_PDU_INCOMP_PSTATE:
443 case NS_CAUSE_PROTO_ERR_UNSPEC:
444 case NS_CAUSE_INVAL_ESSENT_IE:
445 case NS_CAUSE_MISSING_ESSENT_IE:
446 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
447 orig_msg->l2h);
448 break;
449 default:
450 break;
451 }
452
453 /* Section 9.2.7.3: Static conditions for BVCI */
454 if (cause == NS_CAUSE_BVCI_UNKNOWN)
455 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
456
457 return gprs_ns_tx(nsvc, msg);
458}
459
Harald Weltea9f23c82011-11-23 15:01:31 +0100460/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
461 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
462 * \param[in] cause Numeric NS Cause value
463 * \returns 0 in case of success
464 */
Harald Welte834f26d2010-05-11 06:20:54 +0200465int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
466{
Harald Welteba4c6662010-05-19 15:38:10 +0200467 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200468 struct gprs_ns_hdr *nsh;
469 uint16_t nsvci = htons(nsvc->nsvci);
470
Harald Weltecca49632012-06-16 17:45:59 +0800471 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200472
Harald Welte834f26d2010-05-11 06:20:54 +0200473 if (!msg)
474 return -ENOMEM;
475
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000476 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
477 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
478
Harald Welte834f26d2010-05-11 06:20:54 +0200479 /* be conservative and mark it as blocked even now! */
480 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200481 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200482
Harald Welte144e0292010-05-13 11:45:07 +0200483 msg->l2h = msgb_put(msg, sizeof(*nsh));
484 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200485 nsh->pdu_type = NS_PDUT_BLOCK;
486
487 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
488 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
489
490 return gprs_ns_tx(nsvc, msg);
491}
492
Harald Weltea9f23c82011-11-23 15:01:31 +0100493/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
494 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
495 * \returns 0 in case of success
496 */
Harald Welte834f26d2010-05-11 06:20:54 +0200497int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
498{
Harald Weltecca49632012-06-16 17:45:59 +0800499 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000500 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
501 nsvc->nsei, nsvc->nsvci);
502
Harald Welte834f26d2010-05-11 06:20:54 +0200503 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
504}
505
Harald Weltea9f23c82011-11-23 15:01:31 +0100506/*! \brief Transmit a NS-ALIVE on a given NS-VC
507 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
508 * \returns 0 in case of success
509 */
Harald Welteb983c312010-05-12 12:11:45 +0000510int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
511{
Harald Weltecca49632012-06-16 17:45:59 +0800512 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000513 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
514 nsvc->nsei, nsvc->nsvci);
515
516 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
517}
518
Harald Weltea9f23c82011-11-23 15:01:31 +0100519/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
520 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
521 * \returns 0 in case of success
522 */
Harald Welteb983c312010-05-12 12:11:45 +0000523int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
524{
Harald Weltecca49632012-06-16 17:45:59 +0800525 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000526 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
527 nsvc->nsei, nsvc->nsvci);
528
529 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
530}
531
Harald Weltefe4ab902010-05-12 17:19:53 +0000532static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
533 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
534 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
535 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200536};
537
Harald Welte7c24b9e2010-05-12 12:21:52 +0000538static const struct value_string timer_mode_strs[] = {
539 { NSVC_TIMER_TNS_RESET, "tns-reset" },
540 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
541 { NSVC_TIMER_TNS_TEST, "tns-test" },
542 { 0, NULL }
543};
544
Harald Welte69a4cf22010-05-03 20:55:10 +0200545static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
546{
Harald Weltefe4ab902010-05-12 17:19:53 +0000547 enum ns_timeout tout = timer_mode_tout[mode];
548 unsigned int seconds = nsvc->nsi->timeout[tout];
549
Harald Weltecca49632012-06-16 17:45:59 +0800550 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000551 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
552 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000553 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000554
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200555 if (osmo_timer_pending(&nsvc->timer))
556 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200557
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200558 gettimeofday(&nsvc->timer_started, NULL);
Harald Welte69a4cf22010-05-03 20:55:10 +0200559 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200560 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200561}
562
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200563static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
564{
565 struct timeval now, elapsed;
566 gettimeofday(&now, NULL);
567 timersub(&now, &nsvc->timer_started, &elapsed);
568
569 return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
570}
571
Harald Welte80405452010-05-03 20:16:13 +0200572static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800573{
574 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000575 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
576 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800577
Harald Weltecca49632012-06-16 17:45:59 +0800578 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000579 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
580 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000581 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000582
Harald Welte80405452010-05-03 20:16:13 +0200583 switch (nsvc->timer_mode) {
584 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800585 /* Tns-alive case: we expired without response ! */
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200586 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]);
Harald Welte9ba50052010-03-14 15:45:01 +0800587 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000588 if (nsvc->alive_retries >
589 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800590 /* mark as dead and blocked */
591 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200592 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
593 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200594 LOGP(DNS, LOGL_NOTICE,
595 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200596 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000597 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200598 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
599 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800600 return;
601 }
Harald Welteb983c312010-05-12 12:11:45 +0000602 /* Tns-test case: send NS-ALIVE PDU */
603 gprs_ns_tx_alive(nsvc);
604 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200605 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200606 break;
607 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800608 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000609 gprs_ns_tx_alive(nsvc);
610 /* start Tns-alive timer (transition into faster
611 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000612 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200613 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
614 break;
615 case NSVC_TIMER_TNS_RESET:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200616 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]);
Harald Welte69a4cf22010-05-03 20:55:10 +0200617 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200618 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200619 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200620 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200621 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200622 case _NSVC_TIMER_NR:
623 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800624 }
Harald Welte9ba50052010-03-14 15:45:01 +0800625}
626
627/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800628static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800629{
Harald Welteba4c6662010-05-19 15:38:10 +0200630 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800631 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200632 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800633
Harald Weltecca49632012-06-16 17:45:59 +0800634 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800635 if (!msg)
636 return -ENOMEM;
637
Harald Weltec5478482010-03-18 00:01:43 +0800638 nsvci = htons(nsvc->nsvci);
639 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800640
Harald Welte144e0292010-05-13 11:45:07 +0200641 msg->l2h = msgb_put(msg, sizeof(*nsh));
642 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800643
644 nsh->pdu_type = NS_PDUT_RESET_ACK;
645
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200646 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200647 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800648
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200649 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
650 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800651
Harald Welte24a655f2010-04-30 19:54:29 +0200652 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800653}
654
Harald Weltea9f23c82011-11-23 15:01:31 +0100655/*! \brief High-level function for transmitting a NS-UNITDATA messsage
656 * \param[in] nsi NS-instance on which we shall transmit
657 * \param[in] msg struct msgb to be trasnmitted
658 *
659 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
660 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
661 * header for the NS-UNITDATA message type and sends it off.
662 *
663 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
664 */
Harald Welte24a655f2010-04-30 19:54:29 +0200665int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800666{
Harald Welte24a655f2010-04-30 19:54:29 +0200667 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800668 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200669 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200670
Jacob Erlbeck69017152013-10-14 12:03:54 +0200671 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200672 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200673 int rc;
674 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
675 LOGP(DNS, LOGL_ERROR,
676 "All NS-VCs for NSEI %u are either dead or blocked!\n",
677 msgb_nsei(msg));
678 rc = -EBUSY;
679 } else {
680 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
681 "to NS-VC!\n", msgb_nsei(msg));
682 rc = -EINVAL;
683 }
684
Harald Weltef47df642010-08-09 21:15:40 +0800685 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200686 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200687 }
Harald Weltecca49632012-06-16 17:45:59 +0800688 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800689
Harald Welteec20ba42010-05-13 12:18:49 +0200690 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
691 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800692 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200693 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800694 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800695 return -EIO;
696 }
697
698 nsh->pdu_type = NS_PDUT_UNITDATA;
699 /* spare octet in data[0] */
700 nsh->data[1] = bvci >> 8;
701 nsh->data[2] = bvci & 0xff;
702
Harald Welte24a655f2010-04-30 19:54:29 +0200703 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800704}
705
706/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200707static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800708{
709 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200710 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800711
Harald Weltec1402a62010-05-12 11:48:44 +0200712 if (nsvc->state & NSE_S_BLOCKED)
713 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
714 0, msg);
715
Harald Welte9ba50052010-03-14 15:45:01 +0800716 /* spare octet in data[0] */
717 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200718 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200719 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800720
721 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200722 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800723}
724
725/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200726static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800727{
Harald Weltef030b212010-04-26 19:18:54 +0200728 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800729 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200730 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800731 int rc;
732
Harald Welte41337832010-05-16 00:24:26 +0200733 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800734
Harald Weltebd33f3d2010-05-30 17:19:38 +0200735 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
736 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200737 if (rc < 0) {
738 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
739 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
740 "Error during TLV Parse\n", nsvc->nsei);
741 return rc;
742 }
Harald Welte9ba50052010-03-14 15:45:01 +0800743
744 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200745 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800746 return -EINVAL;
747 }
748
749 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200750 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800751
752 return 0;
753}
754
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200755/* Replace a nsvc object with another based on NSVCI.
756 * This function replaces looks for a NSVC with the given NSVCI and replaces it
757 * if possible and necessary. If replaced, the former value of *nsvc is
758 * returned in *old_nsvc.
759 * \return != 0 if *nsvc points to a matching NSVC.
760 */
761static int gprs_nsvc_replace_if_found(uint16_t nsvci,
762 struct gprs_nsvc **nsvc,
763 struct gprs_nsvc **old_nsvc)
764{
765 struct gprs_nsvc *matching_nsvc;
766
767 if ((*nsvc)->nsvci == nsvci) {
768 *old_nsvc = NULL;
769 return 1;
770 }
771
772 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
773
774 if (!matching_nsvc)
775 return 0;
776
777 /* The NS-VCI is already used by this NS-VC */
778
779 char *old_peer;
780
781 /* Exchange the NS-VC objects */
782 *old_nsvc = *nsvc;
783 *nsvc = matching_nsvc;
784
785 /* Do logging */
786 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
787 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
788 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
789
790 talloc_free(old_peer);
791
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200792 return 1;
793}
794
Harald Welte9ba50052010-03-14 15:45:01 +0800795/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200796static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800797{
798 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800799 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200800 uint8_t cause;
801 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200802 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800803 int rc;
804
Harald Weltebd33f3d2010-05-30 17:19:38 +0200805 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
806 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200807 if (rc < 0) {
808 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200809 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200810 return rc;
811 }
Harald Welte9ba50052010-03-14 15:45:01 +0800812
813 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
814 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
815 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200816 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200817 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800818 return -EINVAL;
819 }
820
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200821 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
822 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
823 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Harald Welte9ba50052010-03-14 15:45:01 +0800824
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200825 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
826 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
827 nsei, nsvci, gprs_ns_cause_str(cause));
828
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200829 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
830 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200831 /* The incoming RESET doesn't match the NSVCI. Send an
832 * appropriate RESET_ACK and ignore the RESET.
833 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
834 */
835 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
836 NS_PDUT_RESET,
837 NS_IE_VCI);
838 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
839 gprs_ns_tx_reset_ack(*nsvc);
840 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200841 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200842
843 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200844 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
845 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
846 "at %s\n",
847 nsvci, (*nsvc)->nsvci,
848 gprs_ns_ll_str(*nsvc));
849 orig_nsvc = *nsvc;
850 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200851 (*nsvc)->nsei = nsei;
852 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200853 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200854
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200855 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
856 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
857 /* The incoming RESET doesn't match the NSEI. Send an
858 * appropriate RESET_ACK and ignore the RESET.
859 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
860 */
861 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
862 NS_PDUT_RESET,
863 NS_IE_NSEI);
864 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200865 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200866 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200867 return 0;
868 }
869
870 /* NSEI has changed */
871 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
872 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200873 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200874
Harald Weltec1402a62010-05-12 11:48:44 +0200875 /* Mark NS-VC as blocked and alive */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200876 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200877
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200878 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200879 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200880 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200881
882 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200883 gprs_ns_ll_copy(*nsvc, orig_nsvc);
884 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200885 } else {
886 (*nsvc)->nsei = nsei;
887 (*nsvc)->nsvci = nsvci;
888 (*nsvc)->nsvci_is_valid = 1;
889 }
Harald Welte9ba50052010-03-14 15:45:01 +0800890
Harald Welte834f26d2010-05-11 06:20:54 +0200891 /* inform interested parties about the fact that this NSVC
892 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200893 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200894
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200895 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200896
897 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200898 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
899 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200900
901 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800902}
903
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200904static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
905{
906 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
907 struct tlv_parsed tp;
908 uint16_t nsvci, nsei;
909 struct gprs_nsvc *orig_nsvc = NULL;
910 int rc;
911
912 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
913 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
914 if (rc < 0) {
915 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
916 "Error during TLV Parse\n", (*nsvc)->nsei);
917 return rc;
918 }
919
920 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
921 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
922 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +0200923 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200924 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200925 return -EINVAL;
926 }
927
928 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
929 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
930
931 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
932 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
933 nsei, nsvci);
934
935 if (!((*nsvc)->state & NSE_S_RESET)) {
936 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
937 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
938 */
939 LOGP(DNS, LOGL_ERROR,
940 "NS RESET ACK Discarding unexpected message for "
941 "NS-VCI %d from SGSN NSEI=%d\n",
942 nsvci, nsei);
943 return 0;
944 }
945
946 if (!(*nsvc)->nsvci_is_valid) {
947 LOGP(DNS, LOGL_NOTICE,
948 "NS RESET ACK Uninitialised NS-VC (%u) for "
949 "NS-VCI %d, NSEI=%d from %s\n",
950 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
951 return -EINVAL;
952 }
953
954 if ((*nsvc)->nsvci != nsvci) {
955 /* NS-VCI has changed */
956
957 /* if !0, use another NSVC object that matches the NSVCI */
958 int use_other_nsvc;
959
960 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200961 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
962 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200963
964 if (use_other_nsvc)
965 /* Update *nsvc to point to the right NSVC object */
966 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
967 &orig_nsvc);
968
969 if (!use_other_nsvc) {
970 /* The incoming RESET_ACK doesn't match the NSVCI.
971 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
972 */
973 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
974 NS_PDUT_RESET_ACK,
975 NS_IE_VCI);
976 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
977 LOGP(DNS, LOGL_ERROR,
978 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
979 "from %s\n",
980 nsvci,
981 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
982 nsei, gprs_ns_ll_str(*nsvc));
983 return -EINVAL;
984 }
985
986 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200987 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200988 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
989
990 /* Update the ll info fields */
991 gprs_ns_ll_copy(*nsvc, orig_nsvc);
992 gprs_ns_ll_clear(orig_nsvc);
993 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200994 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
995 /* The incoming RESET_ACK doesn't match the NSEI.
996 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
997 */
998 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
999 NS_PDUT_RESET_ACK,
1000 NS_IE_NSEI);
1001 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
1002 LOGP(DNS, LOGL_ERROR,
1003 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
1004 nsei, nsvci, gprs_ns_ll_str(*nsvc));
1005 return -EINVAL;
1006 }
1007
1008 /* NSEI has changed */
1009 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
1010 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001011 }
1012
1013 /* Mark NS-VC as blocked and alive */
1014 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
1015 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
1016 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1017 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
1018 /* stop RESET timer */
1019 osmo_timer_del(&(*nsvc)->timer);
1020 }
1021 /* Initiate TEST proc.: Send ALIVE and start timer */
1022 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
1023 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1024
1025 return rc;
1026}
1027
Harald Welte834f26d2010-05-11 06:20:54 +02001028static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
1029{
1030 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1031 struct tlv_parsed tp;
1032 uint8_t *cause;
1033 int rc;
1034
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001035 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +02001036
1037 nsvc->state |= NSE_S_BLOCKED;
1038
Harald Weltebd33f3d2010-05-30 17:19:38 +02001039 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1040 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +02001041 if (rc < 0) {
1042 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
1043 "Error during TLV Parse\n", nsvc->nsei);
1044 return rc;
1045 }
Harald Welte834f26d2010-05-11 06:20:54 +02001046
1047 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1048 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +02001049 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +02001050 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +02001051 return -EINVAL;
1052 }
1053
1054 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1055 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1056
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001057 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001058 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001059
1060 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
1061}
1062
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001063int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1064 struct gprs_nsvc *fallback_nsvc,
1065 struct gprs_nsvc **new_nsvc);
1066
1067int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001068 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001069
Harald Weltea9f23c82011-11-23 15:01:31 +01001070/*! \brief Receive incoming NS message from underlying transport layer
1071 * \param nsi NS instance to which the data belongs
1072 * \param[in] msg message buffer containing newly-received data
1073 * \param[in] saddr socketaddr from which data was received
1074 * \param[in] ll link-layer type in which data was received
1075 * \returns 0 in case of success, < 0 in case of error
1076 *
1077 * This is the main entry point int othe NS imlementation where frames
1078 * from the underlying transport (normally UDP) enter.
1079 */
Harald Weltef030b212010-04-26 19:18:54 +02001080int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001081 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001082{
Harald Weltef030b212010-04-26 19:18:54 +02001083 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001084 int rc = 0;
1085
Harald Weltef030b212010-04-26 19:18:54 +02001086 /* look up the NSVC based on source address */
1087 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001088
Harald Weltef030b212010-04-26 19:18:54 +02001089 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001090 struct gprs_nsvc *fallback_nsvc;
1091
1092 fallback_nsvc = nsi->unknown_nsvc;
1093 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1094 fallback_nsvc->ip.bts_addr = *saddr;
1095 fallback_nsvc->ll = ll;
1096
1097 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1098
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001099 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001100 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001101
1102 rc = 0;
1103 }
1104
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001105 if (nsvc)
1106 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001107
1108 return rc;
1109}
1110
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001111const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001112{
1113 static char buf[80];
1114 snprintf(buf, sizeof(buf), "%s:%u",
1115 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
1116 ntohs(nsvc->ip.bts_addr.sin_port));
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001117 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001118
1119 return buf;
1120}
1121
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001122void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1123{
1124 nsvc->ll = other->ll;
1125
1126 switch (nsvc->ll) {
1127 case GPRS_NS_LL_UDP:
1128 nsvc->ip = other->ip;
1129 break;
1130 case GPRS_NS_LL_FR_GRE:
1131 nsvc->frgre = other->frgre;
1132 break;
1133 default:
1134 break;
1135 }
1136}
1137
1138void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1139{
1140 switch (nsvc->ll) {
1141 case GPRS_NS_LL_UDP:
1142 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1143 nsvc->ip.bts_addr.sin_port = 0;
1144 break;
1145 case GPRS_NS_LL_FR_GRE:
1146 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1147 nsvc->frgre.bts_addr.sin_port = 0;
1148 break;
1149 default:
1150 break;
1151 }
1152}
1153
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001154/*! \brief Create/get NS-VC independently from underlying transport layer
1155 * \param nsi NS instance to which the data belongs
1156 * \param[in] msg message buffer containing newly-received data
1157 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001158 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001159 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1160 * been created or found
1161 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1162 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1163 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1164 * has been created and registered, and GPRS_NS_CS_FOUND if an
1165 * existing NS-VC object has been found with the same NSEI.
1166 *
1167 * This contains the initial NS automaton state (NS-VC not yet attached).
1168 */
1169int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1170 struct gprs_nsvc *fallback_nsvc,
1171 struct gprs_nsvc **new_nsvc)
1172{
1173 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1174 struct gprs_nsvc *existing_nsvc;
1175
1176 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001177 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001178 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001179
1180 int rc;
1181
1182 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001183 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001184 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1185 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001186 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001187 return GPRS_NS_CS_SKIPPED;
1188 }
1189
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001190 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1191 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1192 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1193 "for non-existing NS-VC\n",
1194 gprs_ns_ll_str(fallback_nsvc));
1195 return GPRS_NS_CS_SKIPPED;
1196 }
1197
1198 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1199 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1200 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1201 "for non-existing NS-VC\n",
1202 gprs_ns_ll_str(fallback_nsvc));
1203 return GPRS_NS_CS_SKIPPED;
1204 }
1205
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001206 /* Only the RESET procedure creates a new NSVC */
1207 if (nsh->pdu_type != NS_PDUT_RESET) {
1208 /* Since we have no NSVC, we have to use a fake */
1209 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1210 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
1211 "from %s for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001212 nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001213 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001214 fallback_nsvc->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001215 fallback_nsvc->state = NSE_S_ALIVE;
1216
1217 rc = gprs_ns_tx_status(fallback_nsvc,
1218 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1219 if (rc < 0) {
1220 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001221 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001222 return rc;
1223 }
1224 return GPRS_NS_CS_REJECTED;
1225 }
1226
1227 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1228 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1229 if (rc < 0) {
1230 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1231 "TLV Parse\n", rc);
1232 return rc;
1233 }
1234 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1235 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1236 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001237 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001238 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001239 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001240 return -EINVAL;
1241 }
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001242 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001243 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001244 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001245 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001246 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001247 if (!existing_nsvc) {
1248 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001249 (*new_nsvc)->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001250 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001251 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001252 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001253 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001254
1255 return GPRS_NS_CS_CREATED;
1256 }
1257
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001258 /* Check NSEI */
1259 if (existing_nsvc->nsei != nsei) {
1260 LOGP(DNS, LOGL_NOTICE,
1261 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1262 nsvci, existing_nsvc->nsei, nsei);
1263
1264 /* Override old NSEI */
1265 existing_nsvc->nsei = nsei;
1266
1267 /* Do statistics */
1268 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1269 }
1270
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001271 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001272 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001273 return GPRS_NS_CS_FOUND;
1274}
1275
1276/*! \brief Process NS message independently from underlying transport layer
1277 * \param nsi NS instance to which the data belongs
1278 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001279 * \param[inout] nsvc refers to the virtual connection, may be modified when
1280 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001281 * \returns 0 in case of success, < 0 in case of error
1282 *
1283 * This contains the main NS automaton.
1284 */
1285int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001286 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001287{
1288 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1289 int rc = 0;
1290
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001291 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001292
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001293 log_set_context(GPRS_CTX_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001294
Harald Welte144e0292010-05-13 11:45:07 +02001295 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001296 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1297 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001298
Harald Welte9ba50052010-03-14 15:45:01 +08001299 switch (nsh->pdu_type) {
1300 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001301 /* If we're dead and blocked and suddenly receive a
1302 * NS-ALIVE out of the blue, we might have been re-started
1303 * and should send a NS-RESET to make sure everything recovers
1304 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001305 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001306 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1307 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001308 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001309 break;
1310 case NS_PDUT_ALIVE_ACK:
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001311 if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +01001312 osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +02001313 nsvc_timer_elapsed_ms(*nsvc));
Harald Welte90af1942010-05-15 23:02:24 +02001314 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001315 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1316 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001317 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001318 if ((*nsvc)->state & NSE_S_BLOCKED)
1319 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001320 }
Harald Welte9ba50052010-03-14 15:45:01 +08001321 break;
1322 case NS_PDUT_UNITDATA:
1323 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001324 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001325 break;
1326 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001327 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001328 break;
1329 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001330 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001331 break;
1332 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001333 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001334 break;
1335 case NS_PDUT_UNBLOCK:
1336 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001337 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
1338 (*nsvc)->state &= ~NSE_S_BLOCKED;
1339 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1340 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001341 break;
1342 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001343 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001344 /* mark NS-VC as unblocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001345 (*nsvc)->state = NSE_S_ALIVE;
1346 (*nsvc)->remote_state = NSE_S_ALIVE;
1347 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001348 break;
1349 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001350 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001351 break;
1352 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001353 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001354 /* mark remote NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001355 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001356 break;
1357 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001358 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001359 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001360 rc = -EINVAL;
1361 break;
1362 }
1363 return rc;
1364}
1365
Harald Weltea9f23c82011-11-23 15:01:31 +01001366/*! \brief Create a new GPRS NS instance
1367 * \param[in] cb Call-back function for incoming BSSGP data
1368 * \returns dynamically allocated gprs_ns_inst
1369 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001370struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001371{
Harald Welte4fcdd762012-06-16 16:40:42 +08001372 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001373
1374 nsi->cb = cb;
1375 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001376 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1377 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1378 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1379 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1380 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1381 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1382 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001383
Harald Welte60cc6ac2010-05-13 14:20:56 +02001384 /* Create the dummy NSVC that we use for sending
1385 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001386 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001387 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001388 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001389 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001390
Harald Welte3771d092010-04-30 20:26:32 +02001391 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001392}
1393
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001394void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001395{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001396 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001397
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001398 gprs_nsvc_delete(nsi->unknown_nsvc);
1399
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001400 /* delete all NSVCs and clear their timers */
1401 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1402 gprs_nsvc_delete(nsvc);
1403
1404 /* close socket and unregister */
1405 if (nsi->nsip.fd.data) {
1406 close(nsi->nsip.fd.fd);
1407 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001408 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001409 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001410}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001411
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001412/*! \brief Destroy an entire NS instance
1413 * \param nsi gprs_ns_inst that is to be destroyed
1414 *
1415 * This function releases all resources associated with the
1416 * NS-instance.
1417 */
1418void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1419{
1420 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001421 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001422 talloc_free(nsi);
1423}
1424
1425
1426/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1427 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1428
1429/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001430static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001431 struct sockaddr_in *saddr)
1432{
Harald Welteba4c6662010-05-19 15:38:10 +02001433 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001434 int ret = 0;
1435 socklen_t saddr_len = sizeof(*saddr);
1436
1437 if (!msg) {
1438 *error = -ENOMEM;
1439 return NULL;
1440 }
1441
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001442 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001443 (struct sockaddr *)saddr, &saddr_len);
1444 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001445 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001446 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001447 msgb_free(msg);
1448 *error = ret;
1449 return NULL;
1450 } else if (ret == 0) {
1451 msgb_free(msg);
1452 *error = ret;
1453 return NULL;
1454 }
1455
1456 msg->l2h = msg->data;
1457 msgb_put(msg, ret);
1458
1459 return msg;
1460}
1461
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001462static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001463{
1464 int error;
1465 struct sockaddr_in saddr;
1466 struct gprs_ns_inst *nsi = bfd->data;
1467 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1468
1469 if (!msg)
1470 return error;
1471
Harald Welteb3ee2652010-05-19 14:38:50 +02001472 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001473
1474 msgb_free(msg);
1475
1476 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001477}
1478
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001479static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001480{
1481 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1482 return -EIO;
1483}
1484
Harald Welteb3ee2652010-05-19 14:38:50 +02001485static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001486{
1487 int rc;
1488 struct gprs_ns_inst *nsi = nsvc->nsi;
1489 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1490
1491 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1492 (struct sockaddr *)daddr, sizeof(*daddr));
1493
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001494 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001495
1496 return rc;
1497}
1498
1499/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001500static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001501{
1502 int rc = 0;
1503
1504 if (what & BSC_FD_READ)
1505 rc = handle_nsip_read(bfd);
1506 if (what & BSC_FD_WRITE)
1507 rc = handle_nsip_write(bfd);
1508
1509 return rc;
1510}
1511
Harald Weltea9f23c82011-11-23 15:01:31 +01001512/*! \brief Create a listening socket for GPRS NS/UDP/IP
1513 * \param[in] nsi NS protocol instance to listen
1514 * \returns >=0 (fd) in case of success, negative in case of error
1515 *
1516 * A call to this function will create a UDP socket bound to the port
1517 * number and IP address specified in the NS protocol instance. The
1518 * file descriptor of the socket will be stored in nsi->nsip.fd.
1519 */
Harald Welte7fb05232010-05-19 15:09:09 +02001520int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001521{
Harald Welte73952e32012-06-16 14:59:56 +08001522 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001523 int ret;
1524
Harald Welte73952e32012-06-16 14:59:56 +08001525 in.s_addr = htonl(nsi->nsip.local_ip);
1526
1527 nsi->nsip.fd.cb = nsip_fd_cb;
1528 nsi->nsip.fd.data = nsi;
1529 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1530 IPPROTO_UDP, inet_ntoa(in),
1531 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001532 if (ret < 0)
1533 return ret;
1534
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001535 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1536 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1537 if (ret < 0)
1538 LOGP(DNS, LOGL_ERROR,
1539 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1540 nsi->nsip.dscp, ret, errno);
1541
Harald Weltef030b212010-04-26 19:18:54 +02001542
1543 return ret;
1544}
Harald Welte3771d092010-04-30 20:26:32 +02001545
Harald Weltea9f23c82011-11-23 15:01:31 +01001546/*! \brief Initiate a RESET procedure
1547 * \param[in] nsvc NS-VC in which to start the procedure
1548 * \param[in] cause Numeric NS cause value
1549 *
1550 * This is a high-level function initiating a NS-RESET procedure. It
1551 * will not only send a NS-RESET, but also set the state to BLOCKED and
1552 * start the Tns-reset timer.
1553 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001554int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001555{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001556 int rc;
1557
Harald Welteb1020d52010-05-25 22:17:30 +02001558 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1559 nsvc->nsei);
1560
Harald Welte731d1fc2010-05-14 11:53:08 +00001561 /* Mark NS-VC locally as blocked and dead */
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001562 nsvc->state = NSE_S_BLOCKED | NSE_S_RESET;
1563
Harald Welte731d1fc2010-05-14 11:53:08 +00001564 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001565 rc = gprs_ns_tx_reset(nsvc, cause);
1566 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00001567 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1568 nsvc->nsei);
1569 }
1570 /* Start Tns-reset */
1571 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001572
1573 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00001574}
1575
Harald Weltea9f23c82011-11-23 15:01:31 +01001576/*! \brief Establish a NS connection (from the BSS) to the SGSN
1577 * \param nsi NS-instance
1578 * \param[in] dest Destination IP/Port
1579 * \param[in] nsei NSEI of the to-be-established NS-VC
1580 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1581 * \returns struct gprs_nsvc representing the new NS-VC
1582 *
1583 * This function will establish a single NS/UDP/IP connection in uplink
1584 * (BSS to SGSN) direction.
1585 */
Harald Weltef5430362012-06-17 12:25:53 +08001586struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001587 struct sockaddr_in *dest, uint16_t nsei,
1588 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001589{
1590 struct gprs_nsvc *nsvc;
1591
1592 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001593 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001594 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001595 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001596 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02001597 nsvc->remote_end_is_sgsn = 1;
1598
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001599 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1600 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001601}
Harald Weltea9f23c82011-11-23 15:01:31 +01001602
Harald Weltecca49632012-06-16 17:45:59 +08001603void gprs_ns_set_log_ss(int ss)
1604{
1605 DNS = ss;
1606}
1607
Harald Weltea9f23c82011-11-23 15:01:31 +01001608/*! }@ */