blob: 5fd287b07cf25c4fbea9656a55194be2770f0f15 [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,
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200105 NS_CTR_INV_VCI,
106 NS_CTR_INV_NSEI,
Harald Weltec51c23c2010-05-13 12:55:20 +0200107};
108
Harald Welte144e0292010-05-13 11:45:07 +0200109static const struct rate_ctr_desc nsvc_ctr_description[] = {
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200110 { "packets.in", "Packets at NS Level ( In)" },
111 { "packets.out","Packets at NS Level (Out)" },
112 { "bytes.in", "Bytes at NS Level ( In)" },
113 { "bytes.out", "Bytes at NS Level (Out)" },
114 { "blocked", "NS-VC Block count " },
115 { "dead", "NS-VC gone dead count " },
116 { "replaced", "NS-VC replaced other count" },
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200117 { "nsei-chg", "NS-VC changed NSEI count " },
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200118 { "inv-nsvci", "NS-VCI was invalid count " },
119 { "inv-nsei", "NSEI was invalid count " },
Harald Welte144e0292010-05-13 11:45:07 +0200120};
121
122static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
Harald Weltec51c23c2010-05-13 12:55:20 +0200123 .group_name_prefix = "ns.nsvc",
Harald Welte144e0292010-05-13 11:45:07 +0200124 .group_description = "NSVC Peer Statistics",
125 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
126 .ctr_desc = nsvc_ctr_description,
127};
128
Harald Weltea9f23c82011-11-23 15:01:31 +0100129/*! \brief Lookup struct gprs_nsvc based on NSVCI
130 * \param[in] nsi NS instance in which to search
131 * \param[in] nsvci NSVCI to be searched
132 * \returns gprs_nsvc of respective NSVCI
133 */
Harald Weltef5430362012-06-17 12:25:53 +0800134struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200135{
136 struct gprs_nsvc *nsvc;
137 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
138 if (nsvc->nsvci == nsvci)
139 return nsvc;
140 }
141 return NULL;
142}
143
Harald Weltea9f23c82011-11-23 15:01:31 +0100144/*! \brief Lookup struct gprs_nsvc based on NSEI
145 * \param[in] nsi NS instance in which to search
146 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200147 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100148 */
Harald Weltef5430362012-06-17 12:25:53 +0800149struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200150{
151 struct gprs_nsvc *nsvc;
152 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
153 if (nsvc->nsei == nsei)
154 return nsvc;
155 }
156 return NULL;
157}
158
Jacob Erlbeck69017152013-10-14 12:03:54 +0200159static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
160 uint16_t nsei)
161{
162 struct gprs_nsvc *nsvc;
163 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
164 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200165 if (!(nsvc->state & NSE_S_BLOCKED) &&
166 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200167 return nsvc;
168 }
169 }
170 return NULL;
171}
172
Harald Weltef030b212010-04-26 19:18:54 +0200173/* Lookup struct gprs_nsvc based on remote peer socket addr */
174static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
175 struct sockaddr_in *sin)
176{
177 struct gprs_nsvc *nsvc;
178 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000179 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
180 sin->sin_addr.s_addr &&
181 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200182 return nsvc;
183 }
184 return NULL;
185}
186
Harald Welte69a4cf22010-05-03 20:55:10 +0200187static void gprs_ns_timer_cb(void *data);
188
Harald Weltef5430362012-06-17 12:25:53 +0800189struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200190{
191 struct gprs_nsvc *nsvc;
192
Harald Welteb1020d52010-05-25 22:17:30 +0200193 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
194
Harald Weltef030b212010-04-26 19:18:54 +0200195 nsvc = talloc_zero(nsi, struct gprs_nsvc);
196 nsvc->nsvci = nsvci;
197 /* before RESET procedure: BLOCKED and DEAD */
198 nsvc->state = NSE_S_BLOCKED;
199 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200200 nsvc->timer.cb = gprs_ns_timer_cb;
201 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200202 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200203
Harald Weltef030b212010-04-26 19:18:54 +0200204 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
205
206 return nsvc;
207}
Harald Welte9ba50052010-03-14 15:45:01 +0800208
Harald Weltea9f23c82011-11-23 15:01:31 +0100209/*! \brief Delete given NS-VC
210 * \param[in] nsvc gprs_nsvc to be deleted
211 */
Harald Weltef5430362012-06-17 12:25:53 +0800212void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000213{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200214 if (osmo_timer_pending(&nsvc->timer))
215 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000216 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200217 rate_ctr_group_free(nsvc->ctrg);
Harald Welte2bffac52010-05-12 15:55:23 +0000218 talloc_free(nsvc);
219}
220
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200221static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200222 uint8_t cause)
223{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200224 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200225
226 nssd.nsvc = nsvc;
227 nssd.cause = cause;
228
Harald Welte4fcdd762012-06-16 16:40:42 +0800229 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200230}
231
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200232static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
233 struct msgb *msg,
234 uint8_t pdu_type, uint8_t ie_type)
235{
236 struct ns_signal_data nssd = {0};
237
238 nssd.nsvc = nsvc;
239 nssd.pdu_type = pdu_type;
240 nssd.ie_type = ie_type;
241 nssd.msg = msg;
242
243 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
244}
245
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200246static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
247{
248 struct ns_signal_data nssd = {0};
249
250 nssd.nsvc = nsvc;
251 nssd.old_nsvc = old_nsvc;
252
253 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
254}
255
Harald Welte9ba50052010-03-14 15:45:01 +0800256/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200257static const struct value_string ns_cause_str[] = {
258 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
259 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
260 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
261 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
262 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
263 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
264 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
265 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200266 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200267 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
268 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
269 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800270};
271
Harald Weltea9f23c82011-11-23 15:01:31 +0100272/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200273const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800274{
Harald Welte2577d412010-05-02 09:37:45 +0200275 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800276}
277
Harald Welte24a655f2010-04-30 19:54:29 +0200278static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200279extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200280
Harald Welte24a655f2010-04-30 19:54:29 +0200281static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800282{
Harald Weltef030b212010-04-26 19:18:54 +0200283 int ret;
284
Harald Weltecca49632012-06-16 17:45:59 +0800285 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200286
Harald Welte144e0292010-05-13 11:45:07 +0200287 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200288 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
289 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200290
Harald Welteb3ee2652010-05-19 14:38:50 +0200291 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200292 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200293 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200294 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200295 case GPRS_NS_LL_FR_GRE:
296 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
297 break;
Harald Weltef030b212010-04-26 19:18:54 +0200298 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200299 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200300 msgb_free(msg);
301 ret = -EIO;
302 break;
303 }
304 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800305}
306
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200307static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800308{
Harald Welteba4c6662010-05-19 15:38:10 +0200309 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800310 struct gprs_ns_hdr *nsh;
311
Harald Weltecca49632012-06-16 17:45:59 +0800312 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200313
Harald Welte9ba50052010-03-14 15:45:01 +0800314 if (!msg)
315 return -ENOMEM;
316
Harald Welte144e0292010-05-13 11:45:07 +0200317 msg->l2h = msgb_put(msg, sizeof(*nsh));
318 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800319
320 nsh->pdu_type = pdu_type;
321
Harald Welte24a655f2010-04-30 19:54:29 +0200322 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800323}
324
Harald Weltea9f23c82011-11-23 15:01:31 +0100325/*! \brief Transmit a NS-RESET on a given NSVC
326 * \param[in] nsvc NS-VC used for transmission
327 * \paam[in] cause Numeric NS cause value
328 */
Harald Welte834f26d2010-05-11 06:20:54 +0200329int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200330{
Harald Welteba4c6662010-05-19 15:38:10 +0200331 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200332 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200333 uint16_t nsvci = htons(nsvc->nsvci);
334 uint16_t nsei = htons(nsvc->nsei);
335
Harald Weltecca49632012-06-16 17:45:59 +0800336 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200337
Harald Welte69a4cf22010-05-03 20:55:10 +0200338 if (!msg)
339 return -ENOMEM;
340
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000341 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
342 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
343
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200344 nsvc->state |= NSE_S_RESET;
345
Harald Welte144e0292010-05-13 11:45:07 +0200346 msg->l2h = msgb_put(msg, sizeof(*nsh));
347 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200348 nsh->pdu_type = NS_PDUT_RESET;
349
350 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
351 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
352 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
353
354 return gprs_ns_tx(nsvc, msg);
355
356}
357
Harald Weltea9f23c82011-11-23 15:01:31 +0100358/*! \brief Transmit a NS-STATUS on a given NSVC
359 * \param[in] nsvc NS-VC to be used for transmission
360 * \param[in] cause Numeric NS cause value
361 * \param[in] bvci BVCI to be reset within NSVC
362 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200363int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
364 uint16_t bvci, struct msgb *orig_msg)
365{
Harald Welteba4c6662010-05-19 15:38:10 +0200366 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200367 struct gprs_ns_hdr *nsh;
368 uint16_t nsvci = htons(nsvc->nsvci);
369
Harald Weltecca49632012-06-16 17:45:59 +0800370 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200371
Harald Weltec1402a62010-05-12 11:48:44 +0200372 bvci = htons(bvci);
373
374 if (!msg)
375 return -ENOMEM;
376
Harald Welte90af1942010-05-15 23:02:24 +0200377 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000378 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
379
Harald Welte144e0292010-05-13 11:45:07 +0200380 msg->l2h = msgb_put(msg, sizeof(*nsh));
381 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200382 nsh->pdu_type = NS_PDUT_STATUS;
383
384 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
385
386 /* Section 9.2.7.1: Static conditions for NS-VCI */
387 if (cause == NS_CAUSE_NSVC_BLOCKED ||
388 cause == NS_CAUSE_NSVC_UNKNOWN)
389 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
390
391 /* Section 9.2.7.2: Static conditions for NS PDU */
392 switch (cause) {
393 case NS_CAUSE_SEM_INCORR_PDU:
394 case NS_CAUSE_PDU_INCOMP_PSTATE:
395 case NS_CAUSE_PROTO_ERR_UNSPEC:
396 case NS_CAUSE_INVAL_ESSENT_IE:
397 case NS_CAUSE_MISSING_ESSENT_IE:
398 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
399 orig_msg->l2h);
400 break;
401 default:
402 break;
403 }
404
405 /* Section 9.2.7.3: Static conditions for BVCI */
406 if (cause == NS_CAUSE_BVCI_UNKNOWN)
407 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
408
409 return gprs_ns_tx(nsvc, msg);
410}
411
Harald Weltea9f23c82011-11-23 15:01:31 +0100412/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
413 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
414 * \param[in] cause Numeric NS Cause value
415 * \returns 0 in case of success
416 */
Harald Welte834f26d2010-05-11 06:20:54 +0200417int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
418{
Harald Welteba4c6662010-05-19 15:38:10 +0200419 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200420 struct gprs_ns_hdr *nsh;
421 uint16_t nsvci = htons(nsvc->nsvci);
422
Harald Weltecca49632012-06-16 17:45:59 +0800423 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200424
Harald Welte834f26d2010-05-11 06:20:54 +0200425 if (!msg)
426 return -ENOMEM;
427
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000428 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
429 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
430
Harald Welte834f26d2010-05-11 06:20:54 +0200431 /* be conservative and mark it as blocked even now! */
432 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200433 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200434
Harald Welte144e0292010-05-13 11:45:07 +0200435 msg->l2h = msgb_put(msg, sizeof(*nsh));
436 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200437 nsh->pdu_type = NS_PDUT_BLOCK;
438
439 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
440 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
441
442 return gprs_ns_tx(nsvc, msg);
443}
444
Harald Weltea9f23c82011-11-23 15:01:31 +0100445/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
446 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
447 * \returns 0 in case of success
448 */
Harald Welte834f26d2010-05-11 06:20:54 +0200449int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
450{
Harald Weltecca49632012-06-16 17:45:59 +0800451 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000452 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
453 nsvc->nsei, nsvc->nsvci);
454
Harald Welte834f26d2010-05-11 06:20:54 +0200455 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
456}
457
Harald Weltea9f23c82011-11-23 15:01:31 +0100458/*! \brief Transmit a NS-ALIVE on a given NS-VC
459 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
460 * \returns 0 in case of success
461 */
Harald Welteb983c312010-05-12 12:11:45 +0000462int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
463{
Harald Weltecca49632012-06-16 17:45:59 +0800464 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000465 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
466 nsvc->nsei, nsvc->nsvci);
467
468 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
469}
470
Harald Weltea9f23c82011-11-23 15:01:31 +0100471/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
472 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
473 * \returns 0 in case of success
474 */
Harald Welteb983c312010-05-12 12:11:45 +0000475int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
476{
Harald Weltecca49632012-06-16 17:45:59 +0800477 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000478 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
479 nsvc->nsei, nsvc->nsvci);
480
481 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
482}
483
Harald Weltefe4ab902010-05-12 17:19:53 +0000484static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
485 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
486 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
487 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200488};
489
Harald Welte7c24b9e2010-05-12 12:21:52 +0000490static const struct value_string timer_mode_strs[] = {
491 { NSVC_TIMER_TNS_RESET, "tns-reset" },
492 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
493 { NSVC_TIMER_TNS_TEST, "tns-test" },
494 { 0, NULL }
495};
496
Harald Welte69a4cf22010-05-03 20:55:10 +0200497static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
498{
Harald Weltefe4ab902010-05-12 17:19:53 +0000499 enum ns_timeout tout = timer_mode_tout[mode];
500 unsigned int seconds = nsvc->nsi->timeout[tout];
501
Harald Weltecca49632012-06-16 17:45:59 +0800502 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000503 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
504 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000505 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000506
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200507 if (osmo_timer_pending(&nsvc->timer))
508 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200509
510 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200511 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200512}
513
Harald Welte80405452010-05-03 20:16:13 +0200514static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800515{
516 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000517 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
518 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800519
Harald Weltecca49632012-06-16 17:45:59 +0800520 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000521 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
522 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000523 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000524
Harald Welte80405452010-05-03 20:16:13 +0200525 switch (nsvc->timer_mode) {
526 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800527 /* Tns-alive case: we expired without response ! */
528 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000529 if (nsvc->alive_retries >
530 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800531 /* mark as dead and blocked */
532 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200533 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
534 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200535 LOGP(DNS, LOGL_NOTICE,
536 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200537 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000538 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200539 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
540 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800541 return;
542 }
Harald Welteb983c312010-05-12 12:11:45 +0000543 /* Tns-test case: send NS-ALIVE PDU */
544 gprs_ns_tx_alive(nsvc);
545 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200546 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200547 break;
548 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800549 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000550 gprs_ns_tx_alive(nsvc);
551 /* start Tns-alive timer (transition into faster
552 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000553 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200554 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
555 break;
556 case NSVC_TIMER_TNS_RESET:
557 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200558 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200559 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200560 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200561 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200562 case _NSVC_TIMER_NR:
563 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800564 }
Harald Welte9ba50052010-03-14 15:45:01 +0800565}
566
567/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800568static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800569{
Harald Welteba4c6662010-05-19 15:38:10 +0200570 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800571 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200572 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800573
Harald Weltecca49632012-06-16 17:45:59 +0800574 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800575 if (!msg)
576 return -ENOMEM;
577
Harald Weltec5478482010-03-18 00:01:43 +0800578 nsvci = htons(nsvc->nsvci);
579 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800580
Harald Welte144e0292010-05-13 11:45:07 +0200581 msg->l2h = msgb_put(msg, sizeof(*nsh));
582 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800583
584 nsh->pdu_type = NS_PDUT_RESET_ACK;
585
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200586 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200587 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800588
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200589 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
590 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800591
Harald Welte24a655f2010-04-30 19:54:29 +0200592 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800593}
594
Harald Weltea9f23c82011-11-23 15:01:31 +0100595/*! \brief High-level function for transmitting a NS-UNITDATA messsage
596 * \param[in] nsi NS-instance on which we shall transmit
597 * \param[in] msg struct msgb to be trasnmitted
598 *
599 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
600 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
601 * header for the NS-UNITDATA message type and sends it off.
602 *
603 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
604 */
Harald Welte24a655f2010-04-30 19:54:29 +0200605int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800606{
Harald Welte24a655f2010-04-30 19:54:29 +0200607 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800608 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200609 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200610
Jacob Erlbeck69017152013-10-14 12:03:54 +0200611 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200612 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200613 int rc;
614 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
615 LOGP(DNS, LOGL_ERROR,
616 "All NS-VCs for NSEI %u are either dead or blocked!\n",
617 msgb_nsei(msg));
618 rc = -EBUSY;
619 } else {
620 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
621 "to NS-VC!\n", msgb_nsei(msg));
622 rc = -EINVAL;
623 }
624
Harald Weltef47df642010-08-09 21:15:40 +0800625 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200626 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200627 }
Harald Weltecca49632012-06-16 17:45:59 +0800628 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800629
Harald Welteec20ba42010-05-13 12:18:49 +0200630 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
631 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800632 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200633 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800634 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800635 return -EIO;
636 }
637
638 nsh->pdu_type = NS_PDUT_UNITDATA;
639 /* spare octet in data[0] */
640 nsh->data[1] = bvci >> 8;
641 nsh->data[2] = bvci & 0xff;
642
Harald Welte24a655f2010-04-30 19:54:29 +0200643 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800644}
645
646/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200647static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800648{
649 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200650 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800651
Harald Weltec1402a62010-05-12 11:48:44 +0200652 if (nsvc->state & NSE_S_BLOCKED)
653 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
654 0, msg);
655
Harald Welte9ba50052010-03-14 15:45:01 +0800656 /* spare octet in data[0] */
657 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200658 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200659 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800660
661 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200662 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800663}
664
665/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200666static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800667{
Harald Weltef030b212010-04-26 19:18:54 +0200668 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800669 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200670 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800671 int rc;
672
Harald Welte41337832010-05-16 00:24:26 +0200673 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800674
Harald Weltebd33f3d2010-05-30 17:19:38 +0200675 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
676 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200677 if (rc < 0) {
678 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
679 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
680 "Error during TLV Parse\n", nsvc->nsei);
681 return rc;
682 }
Harald Welte9ba50052010-03-14 15:45:01 +0800683
684 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200685 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800686 return -EINVAL;
687 }
688
689 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200690 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800691
692 return 0;
693}
694
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200695/* Replace a nsvc object with another based on NSVCI.
696 * This function replaces looks for a NSVC with the given NSVCI and replaces it
697 * if possible and necessary. If replaced, the former value of *nsvc is
698 * returned in *old_nsvc.
699 * \return != 0 if *nsvc points to a matching NSVC.
700 */
701static int gprs_nsvc_replace_if_found(uint16_t nsvci,
702 struct gprs_nsvc **nsvc,
703 struct gprs_nsvc **old_nsvc)
704{
705 struct gprs_nsvc *matching_nsvc;
706
707 if ((*nsvc)->nsvci == nsvci) {
708 *old_nsvc = NULL;
709 return 1;
710 }
711
712 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
713
714 if (!matching_nsvc)
715 return 0;
716
717 /* The NS-VCI is already used by this NS-VC */
718
719 char *old_peer;
720
721 /* Exchange the NS-VC objects */
722 *old_nsvc = *nsvc;
723 *nsvc = matching_nsvc;
724
725 /* Do logging */
726 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
727 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
728 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
729
730 talloc_free(old_peer);
731
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200732 return 1;
733}
734
Harald Welte9ba50052010-03-14 15:45:01 +0800735/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200736static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800737{
738 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800739 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200740 uint8_t cause;
741 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200742 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800743 int rc;
744
Harald Weltebd33f3d2010-05-30 17:19:38 +0200745 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
746 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200747 if (rc < 0) {
748 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200749 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200750 return rc;
751 }
Harald Welte9ba50052010-03-14 15:45:01 +0800752
753 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
754 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
755 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200756 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200757 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800758 return -EINVAL;
759 }
760
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200761 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
762 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
763 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Harald Welte9ba50052010-03-14 15:45:01 +0800764
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200765 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
766 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
767 nsei, nsvci, gprs_ns_cause_str(cause));
768
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200769 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
770 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200771 /* The incoming RESET doesn't match the NSVCI. Send an
772 * appropriate RESET_ACK and ignore the RESET.
773 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
774 */
775 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
776 NS_PDUT_RESET,
777 NS_IE_VCI);
778 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
779 gprs_ns_tx_reset_ack(*nsvc);
780 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200781 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200782
783 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200784 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
785 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
786 "at %s\n",
787 nsvci, (*nsvc)->nsvci,
788 gprs_ns_ll_str(*nsvc));
789 orig_nsvc = *nsvc;
790 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
791 (*nsvc)->nsvci_is_valid = 1;
792 (*nsvc)->nsei = nsei;
793 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200794 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200795
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200796 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
797 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
798 /* The incoming RESET doesn't match the NSEI. Send an
799 * appropriate RESET_ACK and ignore the RESET.
800 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
801 */
802 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
803 NS_PDUT_RESET,
804 NS_IE_NSEI);
805 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200806 rc = gprs_ns_tx_reset_ack(*nsvc);
807 if (rc < 0)
808 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
809 rc, gprs_ns_ll_str(*nsvc));
810
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200811 return 0;
812 }
813
814 /* NSEI has changed */
815 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
816 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200817 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200818
Harald Weltec1402a62010-05-12 11:48:44 +0200819 /* Mark NS-VC as blocked and alive */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200820 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200821
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200822 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200823 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200824 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200825
826 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200827 gprs_ns_ll_copy(*nsvc, orig_nsvc);
828 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200829 } else {
830 (*nsvc)->nsei = nsei;
831 (*nsvc)->nsvci = nsvci;
832 (*nsvc)->nsvci_is_valid = 1;
833 }
Harald Welte9ba50052010-03-14 15:45:01 +0800834
Harald Welte834f26d2010-05-11 06:20:54 +0200835 /* inform interested parties about the fact that this NSVC
836 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200837 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200838
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200839 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200840
841 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200842 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
843 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200844
845 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800846}
847
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200848static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
849{
850 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
851 struct tlv_parsed tp;
852 uint16_t nsvci, nsei;
853 struct gprs_nsvc *orig_nsvc = NULL;
854 int rc;
855
856 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
857 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
858 if (rc < 0) {
859 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
860 "Error during TLV Parse\n", (*nsvc)->nsei);
861 return rc;
862 }
863
864 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
865 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
866 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +0200867 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
868 if (rc < 0)
869 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
870 rc, gprs_ns_ll_str(*nsvc));
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200871 return -EINVAL;
872 }
873
874 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
875 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
876
877 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
878 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
879 nsei, nsvci);
880
881 if (!((*nsvc)->state & NSE_S_RESET)) {
882 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
883 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
884 */
885 LOGP(DNS, LOGL_ERROR,
886 "NS RESET ACK Discarding unexpected message for "
887 "NS-VCI %d from SGSN NSEI=%d\n",
888 nsvci, nsei);
889 return 0;
890 }
891
892 if (!(*nsvc)->nsvci_is_valid) {
893 LOGP(DNS, LOGL_NOTICE,
894 "NS RESET ACK Uninitialised NS-VC (%u) for "
895 "NS-VCI %d, NSEI=%d from %s\n",
896 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
897 return -EINVAL;
898 }
899
900 if ((*nsvc)->nsvci != nsvci) {
901 /* NS-VCI has changed */
902
903 /* if !0, use another NSVC object that matches the NSVCI */
904 int use_other_nsvc;
905
906 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200907 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
908 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200909
910 if (use_other_nsvc)
911 /* Update *nsvc to point to the right NSVC object */
912 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
913 &orig_nsvc);
914
915 if (!use_other_nsvc) {
916 /* The incoming RESET_ACK doesn't match the NSVCI.
917 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
918 */
919 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
920 NS_PDUT_RESET_ACK,
921 NS_IE_VCI);
922 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
923 LOGP(DNS, LOGL_ERROR,
924 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
925 "from %s\n",
926 nsvci,
927 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
928 nsei, gprs_ns_ll_str(*nsvc));
929 return -EINVAL;
930 }
931
932 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200933 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200934 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
935
936 /* Update the ll info fields */
937 gprs_ns_ll_copy(*nsvc, orig_nsvc);
938 gprs_ns_ll_clear(orig_nsvc);
939 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200940 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
941 /* The incoming RESET_ACK doesn't match the NSEI.
942 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
943 */
944 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
945 NS_PDUT_RESET_ACK,
946 NS_IE_NSEI);
947 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
948 LOGP(DNS, LOGL_ERROR,
949 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
950 nsei, nsvci, gprs_ns_ll_str(*nsvc));
951 return -EINVAL;
952 }
953
954 /* NSEI has changed */
955 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
956 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200957 }
958
959 /* Mark NS-VC as blocked and alive */
960 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
961 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
962 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
963 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
964 /* stop RESET timer */
965 osmo_timer_del(&(*nsvc)->timer);
966 }
967 /* Initiate TEST proc.: Send ALIVE and start timer */
968 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
969 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
970
971 return rc;
972}
973
Harald Welte834f26d2010-05-11 06:20:54 +0200974static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
975{
976 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
977 struct tlv_parsed tp;
978 uint8_t *cause;
979 int rc;
980
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200981 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200982
983 nsvc->state |= NSE_S_BLOCKED;
984
Harald Weltebd33f3d2010-05-30 17:19:38 +0200985 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
986 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200987 if (rc < 0) {
988 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
989 "Error during TLV Parse\n", nsvc->nsei);
990 return rc;
991 }
Harald Welte834f26d2010-05-11 06:20:54 +0200992
993 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
994 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200995 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200996 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200997 return -EINVAL;
998 }
999
1000 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1001 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1002
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001003 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001004 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001005
1006 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
1007}
1008
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001009int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1010 struct gprs_nsvc *fallback_nsvc,
1011 struct gprs_nsvc **new_nsvc);
1012
1013int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001014 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001015
Harald Weltea9f23c82011-11-23 15:01:31 +01001016/*! \brief Receive incoming NS message from underlying transport layer
1017 * \param nsi NS instance to which the data belongs
1018 * \param[in] msg message buffer containing newly-received data
1019 * \param[in] saddr socketaddr from which data was received
1020 * \param[in] ll link-layer type in which data was received
1021 * \returns 0 in case of success, < 0 in case of error
1022 *
1023 * This is the main entry point int othe NS imlementation where frames
1024 * from the underlying transport (normally UDP) enter.
1025 */
Harald Weltef030b212010-04-26 19:18:54 +02001026int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001027 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001028{
Harald Weltef030b212010-04-26 19:18:54 +02001029 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001030 int rc = 0;
1031
Harald Weltef030b212010-04-26 19:18:54 +02001032 /* look up the NSVC based on source address */
1033 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001034
Harald Weltef030b212010-04-26 19:18:54 +02001035 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001036 struct gprs_nsvc *fallback_nsvc;
1037
1038 fallback_nsvc = nsi->unknown_nsvc;
1039 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1040 fallback_nsvc->ip.bts_addr = *saddr;
1041 fallback_nsvc->ll = ll;
1042
1043 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1044
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001045 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001046 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001047
1048 rc = 0;
1049 }
1050
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001051 if (nsvc)
1052 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001053
1054 return rc;
1055}
1056
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001057const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001058{
1059 static char buf[80];
1060 snprintf(buf, sizeof(buf), "%s:%u",
1061 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
1062 ntohs(nsvc->ip.bts_addr.sin_port));
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001063 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001064
1065 return buf;
1066}
1067
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001068void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1069{
1070 nsvc->ll = other->ll;
1071
1072 switch (nsvc->ll) {
1073 case GPRS_NS_LL_UDP:
1074 nsvc->ip = other->ip;
1075 break;
1076 case GPRS_NS_LL_FR_GRE:
1077 nsvc->frgre = other->frgre;
1078 break;
1079 default:
1080 break;
1081 }
1082}
1083
1084void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1085{
1086 switch (nsvc->ll) {
1087 case GPRS_NS_LL_UDP:
1088 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1089 nsvc->ip.bts_addr.sin_port = 0;
1090 break;
1091 case GPRS_NS_LL_FR_GRE:
1092 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1093 nsvc->frgre.bts_addr.sin_port = 0;
1094 break;
1095 default:
1096 break;
1097 }
1098}
1099
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001100/*! \brief Create/get NS-VC independently from underlying transport layer
1101 * \param nsi NS instance to which the data belongs
1102 * \param[in] msg message buffer containing newly-received data
1103 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001104 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001105 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1106 * been created or found
1107 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1108 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1109 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1110 * has been created and registered, and GPRS_NS_CS_FOUND if an
1111 * existing NS-VC object has been found with the same NSEI.
1112 *
1113 * This contains the initial NS automaton state (NS-VC not yet attached).
1114 */
1115int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1116 struct gprs_nsvc *fallback_nsvc,
1117 struct gprs_nsvc **new_nsvc)
1118{
1119 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1120 struct gprs_nsvc *existing_nsvc;
1121
1122 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001123 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001124 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001125
1126 int rc;
1127
1128 if (nsh->pdu_type == NS_PDUT_STATUS) {
1129 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1130 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001131 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001132 return GPRS_NS_CS_SKIPPED;
1133 }
1134
1135 /* Only the RESET procedure creates a new NSVC */
1136 if (nsh->pdu_type != NS_PDUT_RESET) {
1137 /* Since we have no NSVC, we have to use a fake */
1138 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1139 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
1140 "from %s for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001141 nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001142 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001143 fallback_nsvc->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001144 fallback_nsvc->state = NSE_S_ALIVE;
1145
1146 rc = gprs_ns_tx_status(fallback_nsvc,
1147 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1148 if (rc < 0) {
1149 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001150 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001151 return rc;
1152 }
1153 return GPRS_NS_CS_REJECTED;
1154 }
1155
1156 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1157 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1158 if (rc < 0) {
1159 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1160 "TLV Parse\n", rc);
1161 return rc;
1162 }
1163 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1164 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1165 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
1166 gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
1167 msg);
1168 return -EINVAL;
1169 }
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001170 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001171 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001172 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001173 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001174 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001175 if (!existing_nsvc) {
1176 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
1177 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001178 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001179 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001180 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001181
1182 return GPRS_NS_CS_CREATED;
1183 }
1184
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001185 /* Check NSEI */
1186 if (existing_nsvc->nsei != nsei) {
1187 LOGP(DNS, LOGL_NOTICE,
1188 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1189 nsvci, existing_nsvc->nsei, nsei);
1190
1191 /* Override old NSEI */
1192 existing_nsvc->nsei = nsei;
1193
1194 /* Do statistics */
1195 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1196 }
1197
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001198 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001199 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001200 return GPRS_NS_CS_FOUND;
1201}
1202
1203/*! \brief Process NS message independently from underlying transport layer
1204 * \param nsi NS instance to which the data belongs
1205 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001206 * \param[inout] nsvc refers to the virtual connection, may be modified when
1207 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001208 * \returns 0 in case of success, < 0 in case of error
1209 *
1210 * This contains the main NS automaton.
1211 */
1212int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001213 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001214{
1215 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1216 int rc = 0;
1217
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001218 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001219
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001220 log_set_context(GPRS_CTX_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001221
Harald Welte144e0292010-05-13 11:45:07 +02001222 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001223 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1224 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001225
Harald Welte9ba50052010-03-14 15:45:01 +08001226 switch (nsh->pdu_type) {
1227 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001228 /* If we're dead and blocked and suddenly receive a
1229 * NS-ALIVE out of the blue, we might have been re-started
1230 * and should send a NS-RESET to make sure everything recovers
1231 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001232 if ((*nsvc)->state == NSE_S_BLOCKED)
1233 rc = gprs_ns_tx_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
Harald Welte2bffac52010-05-12 15:55:23 +00001234 else
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001235 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001236 break;
1237 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +02001238 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001239 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1240 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001241 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001242 if ((*nsvc)->state & NSE_S_BLOCKED)
1243 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001244 }
Harald Welte9ba50052010-03-14 15:45:01 +08001245 break;
1246 case NS_PDUT_UNITDATA:
1247 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001248 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001249 break;
1250 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001251 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001252 break;
1253 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001254 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001255 break;
1256 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001257 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001258 break;
1259 case NS_PDUT_UNBLOCK:
1260 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001261 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
1262 (*nsvc)->state &= ~NSE_S_BLOCKED;
1263 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1264 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001265 break;
1266 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001267 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001268 /* mark NS-VC as unblocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001269 (*nsvc)->state = NSE_S_ALIVE;
1270 (*nsvc)->remote_state = NSE_S_ALIVE;
1271 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001272 break;
1273 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001274 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001275 break;
1276 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001277 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001278 /* mark remote NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001279 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001280 break;
1281 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001282 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001283 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001284 rc = -EINVAL;
1285 break;
1286 }
1287 return rc;
1288}
1289
Harald Weltea9f23c82011-11-23 15:01:31 +01001290/*! \brief Create a new GPRS NS instance
1291 * \param[in] cb Call-back function for incoming BSSGP data
1292 * \returns dynamically allocated gprs_ns_inst
1293 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001294struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001295{
Harald Welte4fcdd762012-06-16 16:40:42 +08001296 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001297
1298 nsi->cb = cb;
1299 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001300 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1301 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1302 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1303 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1304 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1305 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1306 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001307
Harald Welte60cc6ac2010-05-13 14:20:56 +02001308 /* Create the dummy NSVC that we use for sending
1309 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001310 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +02001311 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001312
Harald Welte3771d092010-04-30 20:26:32 +02001313 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001314}
1315
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001316void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001317{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001318 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001319
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001320 /* delete all NSVCs and clear their timers */
1321 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1322 gprs_nsvc_delete(nsvc);
1323
1324 /* close socket and unregister */
1325 if (nsi->nsip.fd.data) {
1326 close(nsi->nsip.fd.fd);
1327 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001328 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001329 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001330}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001331
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001332/*! \brief Destroy an entire NS instance
1333 * \param nsi gprs_ns_inst that is to be destroyed
1334 *
1335 * This function releases all resources associated with the
1336 * NS-instance.
1337 */
1338void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1339{
1340 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001341 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001342 talloc_free(nsi);
1343}
1344
1345
1346/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1347 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1348
1349/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001350static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001351 struct sockaddr_in *saddr)
1352{
Harald Welteba4c6662010-05-19 15:38:10 +02001353 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001354 int ret = 0;
1355 socklen_t saddr_len = sizeof(*saddr);
1356
1357 if (!msg) {
1358 *error = -ENOMEM;
1359 return NULL;
1360 }
1361
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001362 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001363 (struct sockaddr *)saddr, &saddr_len);
1364 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001365 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001366 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001367 msgb_free(msg);
1368 *error = ret;
1369 return NULL;
1370 } else if (ret == 0) {
1371 msgb_free(msg);
1372 *error = ret;
1373 return NULL;
1374 }
1375
1376 msg->l2h = msg->data;
1377 msgb_put(msg, ret);
1378
1379 return msg;
1380}
1381
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001382static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001383{
1384 int error;
1385 struct sockaddr_in saddr;
1386 struct gprs_ns_inst *nsi = bfd->data;
1387 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1388
1389 if (!msg)
1390 return error;
1391
Harald Welteb3ee2652010-05-19 14:38:50 +02001392 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001393
1394 msgb_free(msg);
1395
1396 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001397}
1398
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001399static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001400{
1401 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1402 return -EIO;
1403}
1404
Harald Welteb3ee2652010-05-19 14:38:50 +02001405static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001406{
1407 int rc;
1408 struct gprs_ns_inst *nsi = nsvc->nsi;
1409 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1410
1411 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1412 (struct sockaddr *)daddr, sizeof(*daddr));
1413
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001414 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001415
1416 return rc;
1417}
1418
1419/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001420static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001421{
1422 int rc = 0;
1423
1424 if (what & BSC_FD_READ)
1425 rc = handle_nsip_read(bfd);
1426 if (what & BSC_FD_WRITE)
1427 rc = handle_nsip_write(bfd);
1428
1429 return rc;
1430}
1431
Harald Weltea9f23c82011-11-23 15:01:31 +01001432/*! \brief Create a listening socket for GPRS NS/UDP/IP
1433 * \param[in] nsi NS protocol instance to listen
1434 * \returns >=0 (fd) in case of success, negative in case of error
1435 *
1436 * A call to this function will create a UDP socket bound to the port
1437 * number and IP address specified in the NS protocol instance. The
1438 * file descriptor of the socket will be stored in nsi->nsip.fd.
1439 */
Harald Welte7fb05232010-05-19 15:09:09 +02001440int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001441{
Harald Welte73952e32012-06-16 14:59:56 +08001442 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001443 int ret;
1444
Harald Welte73952e32012-06-16 14:59:56 +08001445 in.s_addr = htonl(nsi->nsip.local_ip);
1446
1447 nsi->nsip.fd.cb = nsip_fd_cb;
1448 nsi->nsip.fd.data = nsi;
1449 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1450 IPPROTO_UDP, inet_ntoa(in),
1451 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001452 if (ret < 0)
1453 return ret;
1454
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001455 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1456 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1457 if (ret < 0)
1458 LOGP(DNS, LOGL_ERROR,
1459 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1460 nsi->nsip.dscp, ret, errno);
1461
Harald Weltef030b212010-04-26 19:18:54 +02001462
1463 return ret;
1464}
Harald Welte3771d092010-04-30 20:26:32 +02001465
Harald Weltea9f23c82011-11-23 15:01:31 +01001466/*! \brief Initiate a RESET procedure
1467 * \param[in] nsvc NS-VC in which to start the procedure
1468 * \param[in] cause Numeric NS cause value
1469 *
1470 * This is a high-level function initiating a NS-RESET procedure. It
1471 * will not only send a NS-RESET, but also set the state to BLOCKED and
1472 * start the Tns-reset timer.
1473 */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001474void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001475{
Harald Welteb1020d52010-05-25 22:17:30 +02001476 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1477 nsvc->nsei);
1478
Harald Welte731d1fc2010-05-14 11:53:08 +00001479 /* Mark NS-VC locally as blocked and dead */
1480 nsvc->state = NSE_S_BLOCKED;
1481 /* Send NS-RESET PDU */
1482 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
1483 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1484 nsvc->nsei);
1485 }
1486 /* Start Tns-reset */
1487 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +00001488}
1489
Harald Weltea9f23c82011-11-23 15:01:31 +01001490/*! \brief Establish a NS connection (from the BSS) to the SGSN
1491 * \param nsi NS-instance
1492 * \param[in] dest Destination IP/Port
1493 * \param[in] nsei NSEI of the to-be-established NS-VC
1494 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1495 * \returns struct gprs_nsvc representing the new NS-VC
1496 *
1497 * This function will establish a single NS/UDP/IP connection in uplink
1498 * (BSS to SGSN) direction.
1499 */
Harald Weltef5430362012-06-17 12:25:53 +08001500struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001501 struct sockaddr_in *dest, uint16_t nsei,
1502 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001503{
1504 struct gprs_nsvc *nsvc;
1505
1506 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001507 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001508 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001509 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001510 nsvc->nsei = nsei;
1511 nsvc->nsvci = nsvci;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001512 nsvc->nsvci_is_valid = 1;
Harald Welte3771d092010-04-30 20:26:32 +02001513 nsvc->remote_end_is_sgsn = 1;
1514
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001515 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1516 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001517}
Harald Weltea9f23c82011-11-23 15:01:31 +01001518
Harald Weltecca49632012-06-16 17:45:59 +08001519void gprs_ns_set_log_ss(int ss)
1520{
1521 DNS = ss;
1522}
1523
Harald Weltea9f23c82011-11-23 15:01:31 +01001524/*! }@ */