blob: a4a6376fd669368607b75a6caaf2975be110c965 [file] [log] [blame]
Harald Weltea9f23c82011-11-23 15:01:31 +01001/* GPRS Networks Service (NS) messages on the Gb interface
Harald Welte9ba50052010-03-14 15:45:01 +08002 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
Harald Welte605ac5d2012-06-16 16:09:52 +08004/* (C) 2009-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Weltee4cbb3f2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Weltee4cbb3f2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080017 *
Harald Weltee4cbb3f2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080020 *
21 */
22
Harald Weltea9f23c82011-11-23 15:01:31 +010023/*! \addtogroup libgb
24 * @{
25 */
26
27/*! \file gprs_ns.c */
28
29/*!
30 * GPRS Networks Service (NS) messages on the Gb interface
31 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
32 *
33 * Some introduction into NS: NS is used typically on top of frame relay,
Harald Welte9ba50052010-03-14 15:45:01 +080034 * but in the ip.access world it is encapsulated in UDP packets. It serves
35 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
36 * do much, apart from providing congestion notification and status indication.
37 *
38 * Terms:
39 * NS Network Service
40 * NSVC NS Virtual Connection
41 * NSEI NS Entity Identifier
42 * NSVL NS Virtual Link
43 * NSVLI NS Virtual Link Identifier
44 * BVC BSSGP Virtual Connection
45 * BVCI BSSGP Virtual Connection Identifier
46 * NSVCG NS Virtual Connection Goup
47 * Blocked NS-VC cannot be used for user traffic
48 * Alive Ability of a NS-VC to provide communication
49 *
50 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
51 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
52 * NS then has to firgure out which NSVC's are responsible for this BVCI.
53 * Those mappings are administratively configured.
54 */
55
Harald Weltedd1c83c2010-05-13 13:58:08 +020056/* This implementation has the following limitations:
57 * o Only one NS-VC for each NSE: No load-sharing function
58 * o NSVCI 65535 and 65534 are reserved for internal use
59 * o Only UDP is supported as of now, no frame relay support
60 * o The IP Sub-Network-Service (SNS) as specified in 48.016 is not implemented
61 * o There are no BLOCK and UNBLOCK timers (yet?)
62 */
63
Harald Welte9ba50052010-03-14 15:45:01 +080064#include <stdlib.h>
65#include <unistd.h>
66#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020067#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080068
Holger Hans Peter Freyther3c16de22012-07-12 14:13:41 +020069#include <sys/types.h>
70#include <sys/socket.h>
Harald Welte9ba50052010-03-14 15:45:01 +080071#include <arpa/inet.h>
72
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010073#include <osmocom/core/msgb.h>
74#include <osmocom/gsm/tlv.h>
75#include <osmocom/core/talloc.h>
76#include <osmocom/core/select.h>
77#include <osmocom/core/rate_ctr.h>
Harald Welte73952e32012-06-16 14:59:56 +080078#include <osmocom/core/socket.h>
Harald Welte4fcdd762012-06-16 16:40:42 +080079#include <osmocom/core/signal.h>
Harald Welte73952e32012-06-16 14:59:56 +080080#include <osmocom/gprs/gprs_ns.h>
81#include <osmocom/gprs/gprs_bssgp.h>
82#include <osmocom/gprs/gprs_ns_frgre.h>
83
Harald Weltecca49632012-06-16 17:45:59 +080084#include "common_vty.h"
Harald Welte9ba50052010-03-14 15:45:01 +080085
Harald Welte9ba50052010-03-14 15:45:01 +080086static const struct tlv_definition ns_att_tlvdef = {
87 .def = {
88 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
89 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
90 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
91 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
92 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
93 },
94};
95
Harald Weltec51c23c2010-05-13 12:55:20 +020096enum ns_ctr {
97 NS_CTR_PKTS_IN,
98 NS_CTR_PKTS_OUT,
99 NS_CTR_BYTES_IN,
100 NS_CTR_BYTES_OUT,
101 NS_CTR_BLOCKED,
102 NS_CTR_DEAD,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200103 NS_CTR_REPLACED,
104 NS_CTR_NSEI_CHG,
Harald Weltec51c23c2010-05-13 12:55:20 +0200105};
106
Harald Welte144e0292010-05-13 11:45:07 +0200107static const struct rate_ctr_desc nsvc_ctr_description[] = {
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200108 { "packets.in", "Packets at NS Level ( In)" },
109 { "packets.out","Packets at NS Level (Out)" },
110 { "bytes.in", "Bytes at NS Level ( In)" },
111 { "bytes.out", "Bytes at NS Level (Out)" },
112 { "blocked", "NS-VC Block count " },
113 { "dead", "NS-VC gone dead count " },
114 { "replaced", "NS-VC replaced other count" },
115 { "nsei-chg", "NS-VC changed NSEI " },
Harald Welte144e0292010-05-13 11:45:07 +0200116};
117
118static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200119 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200120 .group_description = "NSVC Peer Statistics",
121 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
122 .ctr_desc = nsvc_ctr_description,
123};
124
Harald Weltea9f23c82011-11-23 15:01:31 +0100125/*! \brief Lookup struct gprs_nsvc based on NSVCI
126 * \param[in] nsi NS instance in which to search
127 * \param[in] nsvci NSVCI to be searched
128 * \returns gprs_nsvc of respective NSVCI
129 */
Harald Weltef5430362012-06-17 12:25:53 +0800130struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200131{
132 struct gprs_nsvc *nsvc;
133 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
134 if (nsvc->nsvci == nsvci)
135 return nsvc;
136 }
137 return NULL;
138}
139
Harald Weltea9f23c82011-11-23 15:01:31 +0100140/*! \brief Lookup struct gprs_nsvc based on NSEI
141 * \param[in] nsi NS instance in which to search
142 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200143 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100144 */
Harald Weltef5430362012-06-17 12:25:53 +0800145struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200146{
147 struct gprs_nsvc *nsvc;
148 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
149 if (nsvc->nsei == nsei)
150 return nsvc;
151 }
152 return NULL;
153}
154
Jacob Erlbeck69017152013-10-14 12:03:54 +0200155static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
156 uint16_t nsei)
157{
158 struct gprs_nsvc *nsvc;
159 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
160 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200161 if (!(nsvc->state & NSE_S_BLOCKED) &&
162 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200163 return nsvc;
164 }
165 }
166 return NULL;
167}
168
Harald Weltef030b212010-04-26 19:18:54 +0200169/* Lookup struct gprs_nsvc based on remote peer socket addr */
170static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
171 struct sockaddr_in *sin)
172{
173 struct gprs_nsvc *nsvc;
174 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000175 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
176 sin->sin_addr.s_addr &&
177 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200178 return nsvc;
179 }
180 return NULL;
181}
182
Harald Welte69a4cf22010-05-03 20:55:10 +0200183static void gprs_ns_timer_cb(void *data);
184
Harald Weltef5430362012-06-17 12:25:53 +0800185struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200186{
187 struct gprs_nsvc *nsvc;
188
Harald Welteb1020d52010-05-25 22:17:30 +0200189 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
190
Harald Weltef030b212010-04-26 19:18:54 +0200191 nsvc = talloc_zero(nsi, struct gprs_nsvc);
192 nsvc->nsvci = nsvci;
193 /* before RESET procedure: BLOCKED and DEAD */
194 nsvc->state = NSE_S_BLOCKED;
195 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200196 nsvc->timer.cb = gprs_ns_timer_cb;
197 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200198 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200199
Harald Weltef030b212010-04-26 19:18:54 +0200200 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
201
202 return nsvc;
203}
Harald Welte9ba50052010-03-14 15:45:01 +0800204
Harald Weltea9f23c82011-11-23 15:01:31 +0100205/*! \brief Delete given NS-VC
206 * \param[in] nsvc gprs_nsvc to be deleted
207 */
Harald Weltef5430362012-06-17 12:25:53 +0800208void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000209{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200210 if (osmo_timer_pending(&nsvc->timer))
211 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000212 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200213 rate_ctr_group_free(nsvc->ctrg);
Harald Welte2bffac52010-05-12 15:55:23 +0000214 talloc_free(nsvc);
215}
216
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200217static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200218 uint8_t cause)
219{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200220 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200221
222 nssd.nsvc = nsvc;
223 nssd.cause = cause;
224
Harald Welte4fcdd762012-06-16 16:40:42 +0800225 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200226}
227
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200228static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
229{
230 struct ns_signal_data nssd = {0};
231
232 nssd.nsvc = nsvc;
233 nssd.old_nsvc = old_nsvc;
234
235 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
236}
237
Harald Welte9ba50052010-03-14 15:45:01 +0800238/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200239static const struct value_string ns_cause_str[] = {
240 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
241 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
242 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
243 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
244 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
245 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
246 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
247 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200248 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200249 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
250 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
251 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800252};
253
Harald Weltea9f23c82011-11-23 15:01:31 +0100254/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200255const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800256{
Harald Welte2577d412010-05-02 09:37:45 +0200257 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800258}
259
Harald Welte24a655f2010-04-30 19:54:29 +0200260static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200261extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200262
Harald Welte24a655f2010-04-30 19:54:29 +0200263static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800264{
Harald Weltef030b212010-04-26 19:18:54 +0200265 int ret;
266
Harald Weltecca49632012-06-16 17:45:59 +0800267 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200268
Harald Welte144e0292010-05-13 11:45:07 +0200269 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200270 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
271 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200272
Harald Welteb3ee2652010-05-19 14:38:50 +0200273 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200274 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200275 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200276 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200277 case GPRS_NS_LL_FR_GRE:
278 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
279 break;
Harald Weltef030b212010-04-26 19:18:54 +0200280 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200281 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200282 msgb_free(msg);
283 ret = -EIO;
284 break;
285 }
286 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800287}
288
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200289static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800290{
Harald Welteba4c6662010-05-19 15:38:10 +0200291 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800292 struct gprs_ns_hdr *nsh;
293
Harald Weltecca49632012-06-16 17:45:59 +0800294 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200295
Harald Welte9ba50052010-03-14 15:45:01 +0800296 if (!msg)
297 return -ENOMEM;
298
Harald Welte144e0292010-05-13 11:45:07 +0200299 msg->l2h = msgb_put(msg, sizeof(*nsh));
300 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800301
302 nsh->pdu_type = pdu_type;
303
Harald Welte24a655f2010-04-30 19:54:29 +0200304 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800305}
306
Harald Weltea9f23c82011-11-23 15:01:31 +0100307/*! \brief Transmit a NS-RESET on a given NSVC
308 * \param[in] nsvc NS-VC used for transmission
309 * \paam[in] cause Numeric NS cause value
310 */
Harald Welte834f26d2010-05-11 06:20:54 +0200311int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200312{
Harald Welteba4c6662010-05-19 15:38:10 +0200313 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200314 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200315 uint16_t nsvci = htons(nsvc->nsvci);
316 uint16_t nsei = htons(nsvc->nsei);
317
Harald Weltecca49632012-06-16 17:45:59 +0800318 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200319
Harald Welte69a4cf22010-05-03 20:55:10 +0200320 if (!msg)
321 return -ENOMEM;
322
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000323 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
324 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
325
Harald Welte144e0292010-05-13 11:45:07 +0200326 msg->l2h = msgb_put(msg, sizeof(*nsh));
327 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200328 nsh->pdu_type = NS_PDUT_RESET;
329
330 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
331 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
332 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
333
334 return gprs_ns_tx(nsvc, msg);
335
336}
337
Harald Weltea9f23c82011-11-23 15:01:31 +0100338/*! \brief Transmit a NS-STATUS on a given NSVC
339 * \param[in] nsvc NS-VC to be used for transmission
340 * \param[in] cause Numeric NS cause value
341 * \param[in] bvci BVCI to be reset within NSVC
342 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200343int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
344 uint16_t bvci, struct msgb *orig_msg)
345{
Harald Welteba4c6662010-05-19 15:38:10 +0200346 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200347 struct gprs_ns_hdr *nsh;
348 uint16_t nsvci = htons(nsvc->nsvci);
349
Harald Weltecca49632012-06-16 17:45:59 +0800350 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200351
Harald Weltec1402a62010-05-12 11:48:44 +0200352 bvci = htons(bvci);
353
354 if (!msg)
355 return -ENOMEM;
356
Harald Welte90af1942010-05-15 23:02:24 +0200357 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000358 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
359
Harald Welte144e0292010-05-13 11:45:07 +0200360 msg->l2h = msgb_put(msg, sizeof(*nsh));
361 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200362 nsh->pdu_type = NS_PDUT_STATUS;
363
364 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
365
366 /* Section 9.2.7.1: Static conditions for NS-VCI */
367 if (cause == NS_CAUSE_NSVC_BLOCKED ||
368 cause == NS_CAUSE_NSVC_UNKNOWN)
369 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
370
371 /* Section 9.2.7.2: Static conditions for NS PDU */
372 switch (cause) {
373 case NS_CAUSE_SEM_INCORR_PDU:
374 case NS_CAUSE_PDU_INCOMP_PSTATE:
375 case NS_CAUSE_PROTO_ERR_UNSPEC:
376 case NS_CAUSE_INVAL_ESSENT_IE:
377 case NS_CAUSE_MISSING_ESSENT_IE:
378 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
379 orig_msg->l2h);
380 break;
381 default:
382 break;
383 }
384
385 /* Section 9.2.7.3: Static conditions for BVCI */
386 if (cause == NS_CAUSE_BVCI_UNKNOWN)
387 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
388
389 return gprs_ns_tx(nsvc, msg);
390}
391
Harald Weltea9f23c82011-11-23 15:01:31 +0100392/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
393 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
394 * \param[in] cause Numeric NS Cause value
395 * \returns 0 in case of success
396 */
Harald Welte834f26d2010-05-11 06:20:54 +0200397int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
398{
Harald Welteba4c6662010-05-19 15:38:10 +0200399 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200400 struct gprs_ns_hdr *nsh;
401 uint16_t nsvci = htons(nsvc->nsvci);
402
Harald Weltecca49632012-06-16 17:45:59 +0800403 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200404
Harald Welte834f26d2010-05-11 06:20:54 +0200405 if (!msg)
406 return -ENOMEM;
407
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000408 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
409 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
410
Harald Welte834f26d2010-05-11 06:20:54 +0200411 /* be conservative and mark it as blocked even now! */
412 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200413 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200414
Harald Welte144e0292010-05-13 11:45:07 +0200415 msg->l2h = msgb_put(msg, sizeof(*nsh));
416 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200417 nsh->pdu_type = NS_PDUT_BLOCK;
418
419 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
420 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
421
422 return gprs_ns_tx(nsvc, msg);
423}
424
Harald Weltea9f23c82011-11-23 15:01:31 +0100425/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
426 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
427 * \returns 0 in case of success
428 */
Harald Welte834f26d2010-05-11 06:20:54 +0200429int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
430{
Harald Weltecca49632012-06-16 17:45:59 +0800431 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000432 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
433 nsvc->nsei, nsvc->nsvci);
434
Harald Welte834f26d2010-05-11 06:20:54 +0200435 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
436}
437
Harald Weltea9f23c82011-11-23 15:01:31 +0100438/*! \brief Transmit a NS-ALIVE on a given NS-VC
439 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
440 * \returns 0 in case of success
441 */
Harald Welteb983c312010-05-12 12:11:45 +0000442int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
443{
Harald Weltecca49632012-06-16 17:45:59 +0800444 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000445 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
446 nsvc->nsei, nsvc->nsvci);
447
448 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
449}
450
Harald Weltea9f23c82011-11-23 15:01:31 +0100451/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
452 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
453 * \returns 0 in case of success
454 */
Harald Welteb983c312010-05-12 12:11:45 +0000455int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
456{
Harald Weltecca49632012-06-16 17:45:59 +0800457 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000458 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
459 nsvc->nsei, nsvc->nsvci);
460
461 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
462}
463
Harald Weltefe4ab902010-05-12 17:19:53 +0000464static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
465 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
466 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
467 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200468};
469
Harald Welte7c24b9e2010-05-12 12:21:52 +0000470static const struct value_string timer_mode_strs[] = {
471 { NSVC_TIMER_TNS_RESET, "tns-reset" },
472 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
473 { NSVC_TIMER_TNS_TEST, "tns-test" },
474 { 0, NULL }
475};
476
Harald Welte69a4cf22010-05-03 20:55:10 +0200477static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
478{
Harald Weltefe4ab902010-05-12 17:19:53 +0000479 enum ns_timeout tout = timer_mode_tout[mode];
480 unsigned int seconds = nsvc->nsi->timeout[tout];
481
Harald Weltecca49632012-06-16 17:45:59 +0800482 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000483 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
484 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000485 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000486
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200487 if (osmo_timer_pending(&nsvc->timer))
488 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200489
490 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200491 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200492}
493
Harald Welte80405452010-05-03 20:16:13 +0200494static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800495{
496 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000497 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
498 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800499
Harald Weltecca49632012-06-16 17:45:59 +0800500 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000501 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
502 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000503 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000504
Harald Welte80405452010-05-03 20:16:13 +0200505 switch (nsvc->timer_mode) {
506 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800507 /* Tns-alive case: we expired without response ! */
508 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000509 if (nsvc->alive_retries >
510 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800511 /* mark as dead and blocked */
512 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200513 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
514 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200515 LOGP(DNS, LOGL_NOTICE,
516 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200517 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000518 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200519 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
520 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800521 return;
522 }
Harald Welteb983c312010-05-12 12:11:45 +0000523 /* Tns-test case: send NS-ALIVE PDU */
524 gprs_ns_tx_alive(nsvc);
525 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200526 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200527 break;
528 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800529 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000530 gprs_ns_tx_alive(nsvc);
531 /* start Tns-alive timer (transition into faster
532 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000533 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200534 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
535 break;
536 case NSVC_TIMER_TNS_RESET:
537 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200538 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200539 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200540 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200541 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200542 case _NSVC_TIMER_NR:
543 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800544 }
Harald Welte9ba50052010-03-14 15:45:01 +0800545}
546
547/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800548static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800549{
Harald Welteba4c6662010-05-19 15:38:10 +0200550 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800551 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200552 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800553
Harald Weltecca49632012-06-16 17:45:59 +0800554 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800555 if (!msg)
556 return -ENOMEM;
557
Harald Weltec5478482010-03-18 00:01:43 +0800558 nsvci = htons(nsvc->nsvci);
559 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800560
Harald Welte144e0292010-05-13 11:45:07 +0200561 msg->l2h = msgb_put(msg, sizeof(*nsh));
562 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800563
564 nsh->pdu_type = NS_PDUT_RESET_ACK;
565
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200566 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200567 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800568
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200569 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
570 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800571
Harald Welte24a655f2010-04-30 19:54:29 +0200572 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800573}
574
Harald Weltea9f23c82011-11-23 15:01:31 +0100575/*! \brief High-level function for transmitting a NS-UNITDATA messsage
576 * \param[in] nsi NS-instance on which we shall transmit
577 * \param[in] msg struct msgb to be trasnmitted
578 *
579 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
580 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
581 * header for the NS-UNITDATA message type and sends it off.
582 *
583 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
584 */
Harald Welte24a655f2010-04-30 19:54:29 +0200585int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800586{
Harald Welte24a655f2010-04-30 19:54:29 +0200587 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800588 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200589 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200590
Jacob Erlbeck69017152013-10-14 12:03:54 +0200591 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200592 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200593 int rc;
594 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
595 LOGP(DNS, LOGL_ERROR,
596 "All NS-VCs for NSEI %u are either dead or blocked!\n",
597 msgb_nsei(msg));
598 rc = -EBUSY;
599 } else {
600 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
601 "to NS-VC!\n", msgb_nsei(msg));
602 rc = -EINVAL;
603 }
604
Harald Weltef47df642010-08-09 21:15:40 +0800605 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200606 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200607 }
Harald Weltecca49632012-06-16 17:45:59 +0800608 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800609
Harald Welteec20ba42010-05-13 12:18:49 +0200610 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
611 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800612 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200613 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800614 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800615 return -EIO;
616 }
617
618 nsh->pdu_type = NS_PDUT_UNITDATA;
619 /* spare octet in data[0] */
620 nsh->data[1] = bvci >> 8;
621 nsh->data[2] = bvci & 0xff;
622
Harald Welte24a655f2010-04-30 19:54:29 +0200623 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800624}
625
626/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200627static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800628{
629 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200630 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800631
Harald Weltec1402a62010-05-12 11:48:44 +0200632 if (nsvc->state & NSE_S_BLOCKED)
633 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
634 0, msg);
635
Harald Welte9ba50052010-03-14 15:45:01 +0800636 /* spare octet in data[0] */
637 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200638 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200639 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800640
641 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200642 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800643}
644
645/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200646static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800647{
Harald Weltef030b212010-04-26 19:18:54 +0200648 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800649 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200650 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800651 int rc;
652
Harald Welte41337832010-05-16 00:24:26 +0200653 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800654
Harald Weltebd33f3d2010-05-30 17:19:38 +0200655 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
656 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200657 if (rc < 0) {
658 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
659 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
660 "Error during TLV Parse\n", nsvc->nsei);
661 return rc;
662 }
Harald Welte9ba50052010-03-14 15:45:01 +0800663
664 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200665 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800666 return -EINVAL;
667 }
668
669 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200670 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800671
672 return 0;
673}
674
675/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200676static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800677{
678 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800679 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200680 uint8_t cause;
681 uint16_t nsvci, nsei;
682 struct gprs_nsvc *other_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800683 int rc;
684
Harald Weltebd33f3d2010-05-30 17:19:38 +0200685 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
686 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200687 if (rc < 0) {
688 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200689 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200690 return rc;
691 }
Harald Welte9ba50052010-03-14 15:45:01 +0800692
693 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
694 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
695 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200696 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200697 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800698 return -EINVAL;
699 }
700
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200701 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
702 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
703 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Harald Welte9ba50052010-03-14 15:45:01 +0800704
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200705 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
706 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
707 nsei, nsvci, gprs_ns_cause_str(cause));
708
709 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
710 /* NS-VCI has changed */
711 other_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
712
713 if (other_nsvc) {
714 /* The NS-VCI is already used by this NS-VC */
715
716 struct gprs_nsvc *tmp_nsvc;
717 char *old_peer;
718
719 /* Exchange the NS-VC objects */
720 tmp_nsvc = *nsvc;
721 *nsvc = other_nsvc;
722 other_nsvc = tmp_nsvc;
723
724 /* Do logging */
725 old_peer = talloc_strdup(other_nsvc,
726 gprs_ns_ll_str(other_nsvc));
727 LOGP(DNS, LOGL_INFO,
728 "NS-VC changed link (NSVCI=%u) from %s to %s\n",
729 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
730
731 talloc_free(old_peer);
732
733 /* Do statistics */
734 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
735 }
736 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200737
Harald Weltec1402a62010-05-12 11:48:44 +0200738 /* Mark NS-VC as blocked and alive */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200739 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200740
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200741 if (other_nsvc) {
742 /* Check NSEI */
743 if ((*nsvc)->nsei != nsei) {
744 LOGP(DNS, LOGL_NOTICE,
745 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
746 nsvci, (*nsvc)->nsei, nsei);
747
748 /* Override old NSEI */
749 (*nsvc)->nsei = nsei;
750
751 /* Do statistics */
752 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
753 }
754
755 ns_osmo_signal_dispatch_replaced(*nsvc, other_nsvc);
756
757 /* Update the ll info fields */
758 gprs_ns_ll_copy(*nsvc, other_nsvc);
759 gprs_ns_ll_clear(other_nsvc);
760 } else {
761 (*nsvc)->nsei = nsei;
762 (*nsvc)->nsvci = nsvci;
763 (*nsvc)->nsvci_is_valid = 1;
764 }
Harald Welte9ba50052010-03-14 15:45:01 +0800765
Harald Welte834f26d2010-05-11 06:20:54 +0200766 /* inform interested parties about the fact that this NSVC
767 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200768 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200769
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200770 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200771
772 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200773 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
774 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200775
776 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800777}
778
Harald Welte834f26d2010-05-11 06:20:54 +0200779static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
780{
781 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
782 struct tlv_parsed tp;
783 uint8_t *cause;
784 int rc;
785
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200786 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200787
788 nsvc->state |= NSE_S_BLOCKED;
789
Harald Weltebd33f3d2010-05-30 17:19:38 +0200790 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
791 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200792 if (rc < 0) {
793 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
794 "Error during TLV Parse\n", nsvc->nsei);
795 return rc;
796 }
Harald Welte834f26d2010-05-11 06:20:54 +0200797
798 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
799 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200800 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200801 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200802 return -EINVAL;
803 }
804
805 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
806 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
807
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200808 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +0200809 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200810
811 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
812}
813
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200814int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
815 struct gprs_nsvc *fallback_nsvc,
816 struct gprs_nsvc **new_nsvc);
817
818int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200819 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200820
Harald Weltea9f23c82011-11-23 15:01:31 +0100821/*! \brief Receive incoming NS message from underlying transport layer
822 * \param nsi NS instance to which the data belongs
823 * \param[in] msg message buffer containing newly-received data
824 * \param[in] saddr socketaddr from which data was received
825 * \param[in] ll link-layer type in which data was received
826 * \returns 0 in case of success, < 0 in case of error
827 *
828 * This is the main entry point int othe NS imlementation where frames
829 * from the underlying transport (normally UDP) enter.
830 */
Harald Weltef030b212010-04-26 19:18:54 +0200831int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +0200832 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +0800833{
Harald Weltef030b212010-04-26 19:18:54 +0200834 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800835 int rc = 0;
836
Harald Weltef030b212010-04-26 19:18:54 +0200837 /* look up the NSVC based on source address */
838 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200839
Harald Weltef030b212010-04-26 19:18:54 +0200840 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200841 struct gprs_nsvc *fallback_nsvc;
842
843 fallback_nsvc = nsi->unknown_nsvc;
844 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
845 fallback_nsvc->ip.bts_addr = *saddr;
846 fallback_nsvc->ll = ll;
847
848 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
849
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200850 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +0200851 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200852
853 rc = 0;
854 }
855
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200856 if (nsvc)
857 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200858
859 return rc;
860}
861
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200862const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200863{
864 static char buf[80];
865 snprintf(buf, sizeof(buf), "%s:%u",
866 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
867 ntohs(nsvc->ip.bts_addr.sin_port));
868
869 return buf;
870}
871
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200872void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
873{
874 nsvc->ll = other->ll;
875
876 switch (nsvc->ll) {
877 case GPRS_NS_LL_UDP:
878 nsvc->ip = other->ip;
879 break;
880 case GPRS_NS_LL_FR_GRE:
881 nsvc->frgre = other->frgre;
882 break;
883 default:
884 break;
885 }
886}
887
888void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
889{
890 switch (nsvc->ll) {
891 case GPRS_NS_LL_UDP:
892 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
893 nsvc->ip.bts_addr.sin_port = 0;
894 break;
895 case GPRS_NS_LL_FR_GRE:
896 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
897 nsvc->frgre.bts_addr.sin_port = 0;
898 break;
899 default:
900 break;
901 }
902}
903
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200904/*! \brief Create/get NS-VC independently from underlying transport layer
905 * \param nsi NS instance to which the data belongs
906 * \param[in] msg message buffer containing newly-received data
907 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200908 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200909 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
910 * been created or found
911 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
912 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
913 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
914 * has been created and registered, and GPRS_NS_CS_FOUND if an
915 * existing NS-VC object has been found with the same NSEI.
916 *
917 * This contains the initial NS automaton state (NS-VC not yet attached).
918 */
919int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
920 struct gprs_nsvc *fallback_nsvc,
921 struct gprs_nsvc **new_nsvc)
922{
923 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
924 struct gprs_nsvc *existing_nsvc;
925
926 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200927 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200928 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200929
930 int rc;
931
932 if (nsh->pdu_type == NS_PDUT_STATUS) {
933 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
934 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200935 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200936 return GPRS_NS_CS_SKIPPED;
937 }
938
939 /* Only the RESET procedure creates a new NSVC */
940 if (nsh->pdu_type != NS_PDUT_RESET) {
941 /* Since we have no NSVC, we have to use a fake */
942 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
943 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
944 "from %s for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200945 nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200946 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
947 fallback_nsvc->state = NSE_S_ALIVE;
948
949 rc = gprs_ns_tx_status(fallback_nsvc,
950 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
951 if (rc < 0) {
952 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200953 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200954 return rc;
955 }
956 return GPRS_NS_CS_REJECTED;
957 }
958
959 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
960 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
961 if (rc < 0) {
962 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
963 "TLV Parse\n", rc);
964 return rc;
965 }
966 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
967 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
968 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
969 gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
970 msg);
971 return -EINVAL;
972 }
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200973 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200974 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200975 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200976 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +0200977 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200978 if (!existing_nsvc) {
979 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
980 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200981 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200982 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200983 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +0200984
985 return GPRS_NS_CS_CREATED;
986 }
987
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200988 /* Check NSEI */
989 if (existing_nsvc->nsei != nsei) {
990 LOGP(DNS, LOGL_NOTICE,
991 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
992 nsvci, existing_nsvc->nsei, nsei);
993
994 /* Override old NSEI */
995 existing_nsvc->nsei = nsei;
996
997 /* Do statistics */
998 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
999 }
1000
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001001 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001002 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001003 return GPRS_NS_CS_FOUND;
1004}
1005
1006/*! \brief Process NS message independently from underlying transport layer
1007 * \param nsi NS instance to which the data belongs
1008 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001009 * \param[inout] nsvc refers to the virtual connection, may be modified when
1010 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001011 * \returns 0 in case of success, < 0 in case of error
1012 *
1013 * This contains the main NS automaton.
1014 */
1015int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001016 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001017{
1018 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1019 int rc = 0;
1020
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001021 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001022
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001023 log_set_context(GPRS_CTX_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001024
Harald Welte144e0292010-05-13 11:45:07 +02001025 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001026 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1027 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001028
Harald Welte9ba50052010-03-14 15:45:01 +08001029 switch (nsh->pdu_type) {
1030 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001031 /* If we're dead and blocked and suddenly receive a
1032 * NS-ALIVE out of the blue, we might have been re-started
1033 * and should send a NS-RESET to make sure everything recovers
1034 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001035 if ((*nsvc)->state == NSE_S_BLOCKED)
1036 rc = gprs_ns_tx_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
Harald Welte2bffac52010-05-12 15:55:23 +00001037 else
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001038 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001039 break;
1040 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +02001041 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001042 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1043 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001044 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001045 if ((*nsvc)->state & NSE_S_BLOCKED)
1046 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001047 }
Harald Welte9ba50052010-03-14 15:45:01 +08001048 break;
1049 case NS_PDUT_UNITDATA:
1050 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001051 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001052 break;
1053 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001054 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001055 break;
1056 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001057 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001058 break;
1059 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001060 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001061 /* mark NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001062 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
1063 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
1064 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
1065 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Harald Welte69a4cf22010-05-03 20:55:10 +02001066 /* stop RESET timer */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001067 osmo_timer_del(&(*nsvc)->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +02001068 }
Harald Welte90af1942010-05-15 23:02:24 +02001069 /* Initiate TEST proc.: Send ALIVE and start timer */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001070 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
1071 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
Harald Welte9ba50052010-03-14 15:45:01 +08001072 break;
1073 case NS_PDUT_UNBLOCK:
1074 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001075 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
1076 (*nsvc)->state &= ~NSE_S_BLOCKED;
1077 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1078 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001079 break;
1080 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001081 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001082 /* mark NS-VC as unblocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001083 (*nsvc)->state = NSE_S_ALIVE;
1084 (*nsvc)->remote_state = NSE_S_ALIVE;
1085 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001086 break;
1087 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001088 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001089 break;
1090 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001091 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001092 /* mark remote NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001093 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001094 break;
1095 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001096 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001097 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001098 rc = -EINVAL;
1099 break;
1100 }
1101 return rc;
1102}
1103
Harald Weltea9f23c82011-11-23 15:01:31 +01001104/*! \brief Create a new GPRS NS instance
1105 * \param[in] cb Call-back function for incoming BSSGP data
1106 * \returns dynamically allocated gprs_ns_inst
1107 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001108struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001109{
Harald Welte4fcdd762012-06-16 16:40:42 +08001110 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001111
1112 nsi->cb = cb;
1113 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001114 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1115 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1116 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1117 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1118 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1119 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1120 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001121
Harald Welte60cc6ac2010-05-13 14:20:56 +02001122 /* Create the dummy NSVC that we use for sending
1123 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001124 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +02001125 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001126
Harald Welte3771d092010-04-30 20:26:32 +02001127 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001128}
1129
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001130void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001131{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001132 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001133
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001134 /* delete all NSVCs and clear their timers */
1135 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1136 gprs_nsvc_delete(nsvc);
1137
1138 /* close socket and unregister */
1139 if (nsi->nsip.fd.data) {
1140 close(nsi->nsip.fd.fd);
1141 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001142 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001143 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001144}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001145
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001146/*! \brief Destroy an entire NS instance
1147 * \param nsi gprs_ns_inst that is to be destroyed
1148 *
1149 * This function releases all resources associated with the
1150 * NS-instance.
1151 */
1152void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1153{
1154 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001155 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001156 talloc_free(nsi);
1157}
1158
1159
1160/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1161 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1162
1163/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001164static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001165 struct sockaddr_in *saddr)
1166{
Harald Welteba4c6662010-05-19 15:38:10 +02001167 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001168 int ret = 0;
1169 socklen_t saddr_len = sizeof(*saddr);
1170
1171 if (!msg) {
1172 *error = -ENOMEM;
1173 return NULL;
1174 }
1175
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001176 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001177 (struct sockaddr *)saddr, &saddr_len);
1178 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001179 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001180 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001181 msgb_free(msg);
1182 *error = ret;
1183 return NULL;
1184 } else if (ret == 0) {
1185 msgb_free(msg);
1186 *error = ret;
1187 return NULL;
1188 }
1189
1190 msg->l2h = msg->data;
1191 msgb_put(msg, ret);
1192
1193 return msg;
1194}
1195
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001196static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001197{
1198 int error;
1199 struct sockaddr_in saddr;
1200 struct gprs_ns_inst *nsi = bfd->data;
1201 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1202
1203 if (!msg)
1204 return error;
1205
Harald Welteb3ee2652010-05-19 14:38:50 +02001206 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001207
1208 msgb_free(msg);
1209
1210 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001211}
1212
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001213static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001214{
1215 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1216 return -EIO;
1217}
1218
Harald Welteb3ee2652010-05-19 14:38:50 +02001219static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001220{
1221 int rc;
1222 struct gprs_ns_inst *nsi = nsvc->nsi;
1223 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1224
1225 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1226 (struct sockaddr *)daddr, sizeof(*daddr));
1227
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001228 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001229
1230 return rc;
1231}
1232
1233/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001234static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001235{
1236 int rc = 0;
1237
1238 if (what & BSC_FD_READ)
1239 rc = handle_nsip_read(bfd);
1240 if (what & BSC_FD_WRITE)
1241 rc = handle_nsip_write(bfd);
1242
1243 return rc;
1244}
1245
Harald Weltea9f23c82011-11-23 15:01:31 +01001246/*! \brief Create a listening socket for GPRS NS/UDP/IP
1247 * \param[in] nsi NS protocol instance to listen
1248 * \returns >=0 (fd) in case of success, negative in case of error
1249 *
1250 * A call to this function will create a UDP socket bound to the port
1251 * number and IP address specified in the NS protocol instance. The
1252 * file descriptor of the socket will be stored in nsi->nsip.fd.
1253 */
Harald Welte7fb05232010-05-19 15:09:09 +02001254int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001255{
Harald Welte73952e32012-06-16 14:59:56 +08001256 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001257 int ret;
1258
Harald Welte73952e32012-06-16 14:59:56 +08001259 in.s_addr = htonl(nsi->nsip.local_ip);
1260
1261 nsi->nsip.fd.cb = nsip_fd_cb;
1262 nsi->nsip.fd.data = nsi;
1263 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1264 IPPROTO_UDP, inet_ntoa(in),
1265 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001266 if (ret < 0)
1267 return ret;
1268
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001269 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1270 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1271 if (ret < 0)
1272 LOGP(DNS, LOGL_ERROR,
1273 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1274 nsi->nsip.dscp, ret, errno);
1275
Harald Weltef030b212010-04-26 19:18:54 +02001276
1277 return ret;
1278}
Harald Welte3771d092010-04-30 20:26:32 +02001279
Harald Weltea9f23c82011-11-23 15:01:31 +01001280/*! \brief Initiate a RESET procedure
1281 * \param[in] nsvc NS-VC in which to start the procedure
1282 * \param[in] cause Numeric NS cause value
1283 *
1284 * This is a high-level function initiating a NS-RESET procedure. It
1285 * will not only send a NS-RESET, but also set the state to BLOCKED and
1286 * start the Tns-reset timer.
1287 */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001288void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001289{
Harald Welteb1020d52010-05-25 22:17:30 +02001290 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1291 nsvc->nsei);
1292
Harald Welte731d1fc2010-05-14 11:53:08 +00001293 /* Mark NS-VC locally as blocked and dead */
1294 nsvc->state = NSE_S_BLOCKED;
1295 /* Send NS-RESET PDU */
1296 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
1297 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1298 nsvc->nsei);
1299 }
1300 /* Start Tns-reset */
1301 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +00001302}
1303
Harald Weltea9f23c82011-11-23 15:01:31 +01001304/*! \brief Establish a NS connection (from the BSS) to the SGSN
1305 * \param nsi NS-instance
1306 * \param[in] dest Destination IP/Port
1307 * \param[in] nsei NSEI of the to-be-established NS-VC
1308 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1309 * \returns struct gprs_nsvc representing the new NS-VC
1310 *
1311 * This function will establish a single NS/UDP/IP connection in uplink
1312 * (BSS to SGSN) direction.
1313 */
Harald Weltef5430362012-06-17 12:25:53 +08001314struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001315 struct sockaddr_in *dest, uint16_t nsei,
1316 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001317{
1318 struct gprs_nsvc *nsvc;
1319
1320 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001321 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001322 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001323 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001324 nsvc->nsei = nsei;
1325 nsvc->nsvci = nsvci;
Harald Welte3771d092010-04-30 20:26:32 +02001326 nsvc->remote_end_is_sgsn = 1;
1327
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001328 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1329 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001330}
Harald Weltea9f23c82011-11-23 15:01:31 +01001331
Harald Weltecca49632012-06-16 17:45:59 +08001332void gprs_ns_set_log_ss(int ss)
1333{
1334 DNS = ss;
1335}
1336
Harald Weltea9f23c82011-11-23 15:01:31 +01001337/*! }@ */