blob: 27968e300941456f449c23deff512504436bb145 [file] [log] [blame]
Harald Weltea9f23c82011-11-23 15:01:31 +01001/* GPRS Networks Service (NS) messages on the Gb interface
Harald Welte9ba50052010-03-14 15:45:01 +08002 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */
3
Harald Welte605ac5d2012-06-16 16:09:52 +08004/* (C) 2009-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +01009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010016 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080017 *
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080020 *
21 */
22
Harald Weltea9f23c82011-11-23 15:01:31 +010023/*! \addtogroup libgb
24 * @{
25 */
26
27/*! \file gprs_ns.c */
28
29/*!
30 * GPRS Networks Service (NS) messages on the Gb interface
31 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
32 *
33 * Some introduction into NS: NS is used typically on top of frame relay,
Harald Welte9ba50052010-03-14 15:45:01 +080034 * but in the ip.access world it is encapsulated in UDP packets. It serves
35 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
36 * do much, apart from providing congestion notification and status indication.
37 *
38 * Terms:
39 * NS Network Service
40 * NSVC NS Virtual Connection
41 * NSEI NS Entity Identifier
42 * NSVL NS Virtual Link
43 * NSVLI NS Virtual Link Identifier
44 * BVC BSSGP Virtual Connection
45 * BVCI BSSGP Virtual Connection Identifier
46 * NSVCG NS Virtual Connection Goup
47 * Blocked NS-VC cannot be used for user traffic
48 * Alive Ability of a NS-VC to provide communication
49 *
50 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
51 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
52 * NS then has to firgure out which NSVC's are responsible for this BVCI.
53 * Those mappings are administratively configured.
54 */
55
Harald Weltedd1c83c2010-05-13 13:58:08 +020056/* This implementation has the following limitations:
57 * o Only one NS-VC for each NSE: No load-sharing function
58 * o NSVCI 65535 and 65534 are reserved for internal use
59 * o Only UDP is supported as of now, no frame relay support
60 * o The IP Sub-Network-Service (SNS) as specified in 48.016 is not implemented
61 * o There are no BLOCK and UNBLOCK timers (yet?)
62 */
63
Harald Welte9ba50052010-03-14 15:45:01 +080064#include <stdlib.h>
65#include <unistd.h>
66#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020067#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080068
Holger Hans Peter Freyther3c16de22012-07-12 14:13:41 +020069#include <sys/types.h>
70#include <sys/socket.h>
Harald Welte9ba50052010-03-14 15:45:01 +080071#include <arpa/inet.h>
72
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010073#include <osmocom/core/msgb.h>
74#include <osmocom/gsm/tlv.h>
75#include <osmocom/core/talloc.h>
76#include <osmocom/core/select.h>
77#include <osmocom/core/rate_ctr.h>
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
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200129#define CHECK_TX_RC(rc, nsvc) \
130 if (rc < 0) \
131 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", \
132 rc, gprs_ns_ll_str(nsvc));
133
134
Harald Weltea9f23c82011-11-23 15:01:31 +0100135/*! \brief Lookup struct gprs_nsvc based on NSVCI
136 * \param[in] nsi NS instance in which to search
137 * \param[in] nsvci NSVCI to be searched
138 * \returns gprs_nsvc of respective NSVCI
139 */
Harald Weltef5430362012-06-17 12:25:53 +0800140struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200141{
142 struct gprs_nsvc *nsvc;
143 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
144 if (nsvc->nsvci == nsvci)
145 return nsvc;
146 }
147 return NULL;
148}
149
Harald Weltea9f23c82011-11-23 15:01:31 +0100150/*! \brief Lookup struct gprs_nsvc based on NSEI
151 * \param[in] nsi NS instance in which to search
152 * \param[in] nsei NSEI to be searched
Jacob Erlbeck69017152013-10-14 12:03:54 +0200153 * \returns first gprs_nsvc of respective NSEI
Harald Weltea9f23c82011-11-23 15:01:31 +0100154 */
Harald Weltef5430362012-06-17 12:25:53 +0800155struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei)
Harald Welte24a655f2010-04-30 19:54:29 +0200156{
157 struct gprs_nsvc *nsvc;
158 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
159 if (nsvc->nsei == nsei)
160 return nsvc;
161 }
162 return NULL;
163}
164
Jacob Erlbeck69017152013-10-14 12:03:54 +0200165static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi,
166 uint16_t nsei)
167{
168 struct gprs_nsvc *nsvc;
169 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
170 if (nsvc->nsei == nsei) {
Jacob Erlbeckbf021962013-10-17 13:58:35 +0200171 if (!(nsvc->state & NSE_S_BLOCKED) &&
172 nsvc->state & NSE_S_ALIVE)
Jacob Erlbeck69017152013-10-14 12:03:54 +0200173 return nsvc;
174 }
175 }
176 return NULL;
177}
178
Harald Weltef030b212010-04-26 19:18:54 +0200179/* Lookup struct gprs_nsvc based on remote peer socket addr */
180static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi,
181 struct sockaddr_in *sin)
182{
183 struct gprs_nsvc *nsvc;
184 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
Harald Welte38407ef2010-05-12 11:56:39 +0000185 if (nsvc->ip.bts_addr.sin_addr.s_addr ==
186 sin->sin_addr.s_addr &&
187 nsvc->ip.bts_addr.sin_port == sin->sin_port)
Harald Weltef030b212010-04-26 19:18:54 +0200188 return nsvc;
189 }
190 return NULL;
191}
192
Harald Welte69a4cf22010-05-03 20:55:10 +0200193static void gprs_ns_timer_cb(void *data);
194
Harald Weltef5430362012-06-17 12:25:53 +0800195struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci)
Harald Weltef030b212010-04-26 19:18:54 +0200196{
197 struct gprs_nsvc *nsvc;
198
Harald Welteb1020d52010-05-25 22:17:30 +0200199 LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci);
200
Harald Weltef030b212010-04-26 19:18:54 +0200201 nsvc = talloc_zero(nsi, struct gprs_nsvc);
202 nsvc->nsvci = nsvci;
Jacob Erlbeck9b591b72013-11-11 09:43:05 +0100203 nsvc->nsvci_is_valid = 1;
Harald Weltef030b212010-04-26 19:18:54 +0200204 /* before RESET procedure: BLOCKED and DEAD */
205 nsvc->state = NSE_S_BLOCKED;
206 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200207 nsvc->timer.cb = gprs_ns_timer_cb;
208 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200209 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200210
Harald Weltef030b212010-04-26 19:18:54 +0200211 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
212
213 return nsvc;
214}
Harald Welte9ba50052010-03-14 15:45:01 +0800215
Harald Weltea9f23c82011-11-23 15:01:31 +0100216/*! \brief Delete given NS-VC
217 * \param[in] nsvc gprs_nsvc to be deleted
218 */
Harald Weltef5430362012-06-17 12:25:53 +0800219void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000220{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200221 if (osmo_timer_pending(&nsvc->timer))
222 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000223 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200224 rate_ctr_group_free(nsvc->ctrg);
Harald Welte2bffac52010-05-12 15:55:23 +0000225 talloc_free(nsvc);
226}
227
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200228static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200229 uint8_t cause)
230{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200231 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200232
233 nssd.nsvc = nsvc;
234 nssd.cause = cause;
235
Harald Welte4fcdd762012-06-16 16:40:42 +0800236 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200237}
238
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200239static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
240 struct msgb *msg,
241 uint8_t pdu_type, uint8_t ie_type)
242{
243 struct ns_signal_data nssd = {0};
244
245 nssd.nsvc = nsvc;
246 nssd.pdu_type = pdu_type;
247 nssd.ie_type = ie_type;
248 nssd.msg = msg;
249
250 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
251}
252
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200253static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
254{
255 struct ns_signal_data nssd = {0};
256
257 nssd.nsvc = nsvc;
258 nssd.old_nsvc = old_nsvc;
259
260 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
261}
262
Harald Welte9ba50052010-03-14 15:45:01 +0800263/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200264static const struct value_string ns_cause_str[] = {
265 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
266 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
267 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
268 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
269 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
270 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
271 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
272 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200273 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200274 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
275 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
276 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800277};
278
Harald Weltea9f23c82011-11-23 15:01:31 +0100279/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200280const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800281{
Harald Welte2577d412010-05-02 09:37:45 +0200282 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800283}
284
Harald Welte24a655f2010-04-30 19:54:29 +0200285static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200286extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200287
Harald Welte24a655f2010-04-30 19:54:29 +0200288static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800289{
Harald Weltef030b212010-04-26 19:18:54 +0200290 int ret;
291
Harald Weltecca49632012-06-16 17:45:59 +0800292 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200293
Harald Welte144e0292010-05-13 11:45:07 +0200294 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200295 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
296 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200297
Harald Welteb3ee2652010-05-19 14:38:50 +0200298 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200299 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200300 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200301 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200302 case GPRS_NS_LL_FR_GRE:
303 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
304 break;
Harald Weltef030b212010-04-26 19:18:54 +0200305 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200306 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200307 msgb_free(msg);
308 ret = -EIO;
309 break;
310 }
311 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800312}
313
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200314static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800315{
Harald Welteba4c6662010-05-19 15:38:10 +0200316 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800317 struct gprs_ns_hdr *nsh;
318
Harald Weltecca49632012-06-16 17:45:59 +0800319 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200320
Harald Welte9ba50052010-03-14 15:45:01 +0800321 if (!msg)
322 return -ENOMEM;
323
Harald Welte144e0292010-05-13 11:45:07 +0200324 msg->l2h = msgb_put(msg, sizeof(*nsh));
325 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800326
327 nsh->pdu_type = pdu_type;
328
Harald Welte24a655f2010-04-30 19:54:29 +0200329 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800330}
331
Harald Weltea9f23c82011-11-23 15:01:31 +0100332/*! \brief Transmit a NS-RESET on a given NSVC
333 * \param[in] nsvc NS-VC used for transmission
334 * \paam[in] cause Numeric NS cause value
335 */
Harald Welte834f26d2010-05-11 06:20:54 +0200336int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200337{
Harald Welteba4c6662010-05-19 15:38:10 +0200338 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200339 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200340 uint16_t nsvci = htons(nsvc->nsvci);
341 uint16_t nsei = htons(nsvc->nsei);
342
Harald Weltecca49632012-06-16 17:45:59 +0800343 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200344
Harald Welte69a4cf22010-05-03 20:55:10 +0200345 if (!msg)
346 return -ENOMEM;
347
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000348 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
349 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
350
Harald Welte144e0292010-05-13 11:45:07 +0200351 msg->l2h = msgb_put(msg, sizeof(*nsh));
352 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200353 nsh->pdu_type = NS_PDUT_RESET;
354
355 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
356 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
357 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
358
359 return gprs_ns_tx(nsvc, msg);
360
361}
362
Harald Weltea9f23c82011-11-23 15:01:31 +0100363/*! \brief Transmit a NS-STATUS on a given NSVC
364 * \param[in] nsvc NS-VC to be used for transmission
365 * \param[in] cause Numeric NS cause value
366 * \param[in] bvci BVCI to be reset within NSVC
367 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200368int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
369 uint16_t bvci, struct msgb *orig_msg)
370{
Harald Welteba4c6662010-05-19 15:38:10 +0200371 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200372 struct gprs_ns_hdr *nsh;
373 uint16_t nsvci = htons(nsvc->nsvci);
374
Harald Weltecca49632012-06-16 17:45:59 +0800375 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200376
Harald Weltec1402a62010-05-12 11:48:44 +0200377 bvci = htons(bvci);
378
379 if (!msg)
380 return -ENOMEM;
381
Harald Welte90af1942010-05-15 23:02:24 +0200382 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000383 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
384
Harald Welte144e0292010-05-13 11:45:07 +0200385 msg->l2h = msgb_put(msg, sizeof(*nsh));
386 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200387 nsh->pdu_type = NS_PDUT_STATUS;
388
389 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
390
391 /* Section 9.2.7.1: Static conditions for NS-VCI */
392 if (cause == NS_CAUSE_NSVC_BLOCKED ||
393 cause == NS_CAUSE_NSVC_UNKNOWN)
394 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
395
396 /* Section 9.2.7.2: Static conditions for NS PDU */
397 switch (cause) {
398 case NS_CAUSE_SEM_INCORR_PDU:
399 case NS_CAUSE_PDU_INCOMP_PSTATE:
400 case NS_CAUSE_PROTO_ERR_UNSPEC:
401 case NS_CAUSE_INVAL_ESSENT_IE:
402 case NS_CAUSE_MISSING_ESSENT_IE:
403 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
404 orig_msg->l2h);
405 break;
406 default:
407 break;
408 }
409
410 /* Section 9.2.7.3: Static conditions for BVCI */
411 if (cause == NS_CAUSE_BVCI_UNKNOWN)
412 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
413
414 return gprs_ns_tx(nsvc, msg);
415}
416
Harald Weltea9f23c82011-11-23 15:01:31 +0100417/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
418 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
419 * \param[in] cause Numeric NS Cause value
420 * \returns 0 in case of success
421 */
Harald Welte834f26d2010-05-11 06:20:54 +0200422int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
423{
Harald Welteba4c6662010-05-19 15:38:10 +0200424 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200425 struct gprs_ns_hdr *nsh;
426 uint16_t nsvci = htons(nsvc->nsvci);
427
Harald Weltecca49632012-06-16 17:45:59 +0800428 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200429
Harald Welte834f26d2010-05-11 06:20:54 +0200430 if (!msg)
431 return -ENOMEM;
432
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000433 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
434 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
435
Harald Welte834f26d2010-05-11 06:20:54 +0200436 /* be conservative and mark it as blocked even now! */
437 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200438 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200439
Harald Welte144e0292010-05-13 11:45:07 +0200440 msg->l2h = msgb_put(msg, sizeof(*nsh));
441 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200442 nsh->pdu_type = NS_PDUT_BLOCK;
443
444 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
445 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
446
447 return gprs_ns_tx(nsvc, msg);
448}
449
Harald Weltea9f23c82011-11-23 15:01:31 +0100450/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
451 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
452 * \returns 0 in case of success
453 */
Harald Welte834f26d2010-05-11 06:20:54 +0200454int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
455{
Harald Weltecca49632012-06-16 17:45:59 +0800456 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000457 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
458 nsvc->nsei, nsvc->nsvci);
459
Harald Welte834f26d2010-05-11 06:20:54 +0200460 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
461}
462
Harald Weltea9f23c82011-11-23 15:01:31 +0100463/*! \brief Transmit a NS-ALIVE on a given NS-VC
464 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
465 * \returns 0 in case of success
466 */
Harald Welteb983c312010-05-12 12:11:45 +0000467int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
468{
Harald Weltecca49632012-06-16 17:45:59 +0800469 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000470 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
471 nsvc->nsei, nsvc->nsvci);
472
473 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
474}
475
Harald Weltea9f23c82011-11-23 15:01:31 +0100476/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
477 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
478 * \returns 0 in case of success
479 */
Harald Welteb983c312010-05-12 12:11:45 +0000480int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
481{
Harald Weltecca49632012-06-16 17:45:59 +0800482 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000483 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
484 nsvc->nsei, nsvc->nsvci);
485
486 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
487}
488
Harald Weltefe4ab902010-05-12 17:19:53 +0000489static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
490 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
491 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
492 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200493};
494
Harald Welte7c24b9e2010-05-12 12:21:52 +0000495static const struct value_string timer_mode_strs[] = {
496 { NSVC_TIMER_TNS_RESET, "tns-reset" },
497 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
498 { NSVC_TIMER_TNS_TEST, "tns-test" },
499 { 0, NULL }
500};
501
Harald Welte69a4cf22010-05-03 20:55:10 +0200502static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
503{
Harald Weltefe4ab902010-05-12 17:19:53 +0000504 enum ns_timeout tout = timer_mode_tout[mode];
505 unsigned int seconds = nsvc->nsi->timeout[tout];
506
Harald Weltecca49632012-06-16 17:45:59 +0800507 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000508 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
509 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000510 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000511
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200512 if (osmo_timer_pending(&nsvc->timer))
513 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200514
515 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200516 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200517}
518
Harald Welte80405452010-05-03 20:16:13 +0200519static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800520{
521 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000522 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
523 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800524
Harald Weltecca49632012-06-16 17:45:59 +0800525 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000526 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
527 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000528 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000529
Harald Welte80405452010-05-03 20:16:13 +0200530 switch (nsvc->timer_mode) {
531 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800532 /* Tns-alive case: we expired without response ! */
533 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000534 if (nsvc->alive_retries >
535 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800536 /* mark as dead and blocked */
537 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200538 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
539 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200540 LOGP(DNS, LOGL_NOTICE,
541 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200542 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000543 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200544 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
545 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800546 return;
547 }
Harald Welteb983c312010-05-12 12:11:45 +0000548 /* Tns-test case: send NS-ALIVE PDU */
549 gprs_ns_tx_alive(nsvc);
550 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200551 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200552 break;
553 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800554 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000555 gprs_ns_tx_alive(nsvc);
556 /* start Tns-alive timer (transition into faster
557 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000558 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200559 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
560 break;
561 case NSVC_TIMER_TNS_RESET:
562 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200563 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200564 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200565 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200566 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200567 case _NSVC_TIMER_NR:
568 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800569 }
Harald Welte9ba50052010-03-14 15:45:01 +0800570}
571
572/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800573static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800574{
Harald Welteba4c6662010-05-19 15:38:10 +0200575 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800576 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200577 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800578
Harald Weltecca49632012-06-16 17:45:59 +0800579 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800580 if (!msg)
581 return -ENOMEM;
582
Harald Weltec5478482010-03-18 00:01:43 +0800583 nsvci = htons(nsvc->nsvci);
584 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800585
Harald Welte144e0292010-05-13 11:45:07 +0200586 msg->l2h = msgb_put(msg, sizeof(*nsh));
587 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800588
589 nsh->pdu_type = NS_PDUT_RESET_ACK;
590
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200591 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200592 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800593
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200594 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
595 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800596
Harald Welte24a655f2010-04-30 19:54:29 +0200597 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800598}
599
Harald Weltea9f23c82011-11-23 15:01:31 +0100600/*! \brief High-level function for transmitting a NS-UNITDATA messsage
601 * \param[in] nsi NS-instance on which we shall transmit
602 * \param[in] msg struct msgb to be trasnmitted
603 *
604 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
605 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
606 * header for the NS-UNITDATA message type and sends it off.
607 *
608 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
609 */
Harald Welte24a655f2010-04-30 19:54:29 +0200610int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800611{
Harald Welte24a655f2010-04-30 19:54:29 +0200612 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800613 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200614 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200615
Jacob Erlbeck69017152013-10-14 12:03:54 +0200616 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200617 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200618 int rc;
619 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
620 LOGP(DNS, LOGL_ERROR,
621 "All NS-VCs for NSEI %u are either dead or blocked!\n",
622 msgb_nsei(msg));
623 rc = -EBUSY;
624 } else {
625 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
626 "to NS-VC!\n", msgb_nsei(msg));
627 rc = -EINVAL;
628 }
629
Harald Weltef47df642010-08-09 21:15:40 +0800630 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200631 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200632 }
Harald Weltecca49632012-06-16 17:45:59 +0800633 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800634
Harald Welteec20ba42010-05-13 12:18:49 +0200635 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
636 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800637 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200638 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800639 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800640 return -EIO;
641 }
642
643 nsh->pdu_type = NS_PDUT_UNITDATA;
644 /* spare octet in data[0] */
645 nsh->data[1] = bvci >> 8;
646 nsh->data[2] = bvci & 0xff;
647
Harald Welte24a655f2010-04-30 19:54:29 +0200648 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800649}
650
651/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200652static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800653{
654 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200655 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800656
Harald Weltec1402a62010-05-12 11:48:44 +0200657 if (nsvc->state & NSE_S_BLOCKED)
658 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
659 0, msg);
660
Harald Welte9ba50052010-03-14 15:45:01 +0800661 /* spare octet in data[0] */
662 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200663 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200664 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800665
666 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200667 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800668}
669
670/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200671static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800672{
Harald Weltef030b212010-04-26 19:18:54 +0200673 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800674 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200675 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800676 int rc;
677
Harald Welte41337832010-05-16 00:24:26 +0200678 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800679
Harald Weltebd33f3d2010-05-30 17:19:38 +0200680 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
681 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200682 if (rc < 0) {
683 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
684 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
685 "Error during TLV Parse\n", nsvc->nsei);
686 return rc;
687 }
Harald Welte9ba50052010-03-14 15:45:01 +0800688
689 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200690 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800691 return -EINVAL;
692 }
693
694 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200695 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800696
697 return 0;
698}
699
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200700/* Replace a nsvc object with another based on NSVCI.
701 * This function replaces looks for a NSVC with the given NSVCI and replaces it
702 * if possible and necessary. If replaced, the former value of *nsvc is
703 * returned in *old_nsvc.
704 * \return != 0 if *nsvc points to a matching NSVC.
705 */
706static int gprs_nsvc_replace_if_found(uint16_t nsvci,
707 struct gprs_nsvc **nsvc,
708 struct gprs_nsvc **old_nsvc)
709{
710 struct gprs_nsvc *matching_nsvc;
711
712 if ((*nsvc)->nsvci == nsvci) {
713 *old_nsvc = NULL;
714 return 1;
715 }
716
717 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
718
719 if (!matching_nsvc)
720 return 0;
721
722 /* The NS-VCI is already used by this NS-VC */
723
724 char *old_peer;
725
726 /* Exchange the NS-VC objects */
727 *old_nsvc = *nsvc;
728 *nsvc = matching_nsvc;
729
730 /* Do logging */
731 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
732 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
733 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
734
735 talloc_free(old_peer);
736
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200737 return 1;
738}
739
Harald Welte9ba50052010-03-14 15:45:01 +0800740/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200741static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800742{
743 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800744 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200745 uint8_t cause;
746 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200747 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800748 int rc;
749
Harald Weltebd33f3d2010-05-30 17:19:38 +0200750 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
751 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200752 if (rc < 0) {
753 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200754 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200755 return rc;
756 }
Harald Welte9ba50052010-03-14 15:45:01 +0800757
758 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
759 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
760 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200761 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200762 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800763 return -EINVAL;
764 }
765
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200766 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
767 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
768 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Harald Welte9ba50052010-03-14 15:45:01 +0800769
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200770 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
771 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
772 nsei, nsvci, gprs_ns_cause_str(cause));
773
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200774 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
775 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200776 /* The incoming RESET doesn't match the NSVCI. Send an
777 * appropriate RESET_ACK and ignore the RESET.
778 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
779 */
780 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
781 NS_PDUT_RESET,
782 NS_IE_VCI);
783 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
784 gprs_ns_tx_reset_ack(*nsvc);
785 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200786 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200787
788 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200789 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
790 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
791 "at %s\n",
792 nsvci, (*nsvc)->nsvci,
793 gprs_ns_ll_str(*nsvc));
794 orig_nsvc = *nsvc;
795 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200796 (*nsvc)->nsei = nsei;
797 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200798 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200799
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200800 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
801 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
802 /* The incoming RESET doesn't match the NSEI. Send an
803 * appropriate RESET_ACK and ignore the RESET.
804 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
805 */
806 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
807 NS_PDUT_RESET,
808 NS_IE_NSEI);
809 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200810 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200811 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200812 return 0;
813 }
814
815 /* NSEI has changed */
816 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
817 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200818 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200819
Harald Weltec1402a62010-05-12 11:48:44 +0200820 /* Mark NS-VC as blocked and alive */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200821 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200822
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200823 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200824 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200825 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200826
827 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200828 gprs_ns_ll_copy(*nsvc, orig_nsvc);
829 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200830 } else {
831 (*nsvc)->nsei = nsei;
832 (*nsvc)->nsvci = nsvci;
833 (*nsvc)->nsvci_is_valid = 1;
834 }
Harald Welte9ba50052010-03-14 15:45:01 +0800835
Harald Welte834f26d2010-05-11 06:20:54 +0200836 /* inform interested parties about the fact that this NSVC
837 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200838 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200839
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200840 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200841
842 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200843 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
844 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200845
846 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800847}
848
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200849static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
850{
851 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
852 struct tlv_parsed tp;
853 uint16_t nsvci, nsei;
854 struct gprs_nsvc *orig_nsvc = NULL;
855 int rc;
856
857 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
858 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
859 if (rc < 0) {
860 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
861 "Error during TLV Parse\n", (*nsvc)->nsei);
862 return rc;
863 }
864
865 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
866 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
867 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +0200868 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200869 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200870 return -EINVAL;
871 }
872
873 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
874 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
875
876 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
877 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
878 nsei, nsvci);
879
880 if (!((*nsvc)->state & NSE_S_RESET)) {
881 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
882 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
883 */
884 LOGP(DNS, LOGL_ERROR,
885 "NS RESET ACK Discarding unexpected message for "
886 "NS-VCI %d from SGSN NSEI=%d\n",
887 nsvci, nsei);
888 return 0;
889 }
890
891 if (!(*nsvc)->nsvci_is_valid) {
892 LOGP(DNS, LOGL_NOTICE,
893 "NS RESET ACK Uninitialised NS-VC (%u) for "
894 "NS-VCI %d, NSEI=%d from %s\n",
895 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
896 return -EINVAL;
897 }
898
899 if ((*nsvc)->nsvci != nsvci) {
900 /* NS-VCI has changed */
901
902 /* if !0, use another NSVC object that matches the NSVCI */
903 int use_other_nsvc;
904
905 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200906 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
907 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200908
909 if (use_other_nsvc)
910 /* Update *nsvc to point to the right NSVC object */
911 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
912 &orig_nsvc);
913
914 if (!use_other_nsvc) {
915 /* The incoming RESET_ACK doesn't match the NSVCI.
916 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
917 */
918 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
919 NS_PDUT_RESET_ACK,
920 NS_IE_VCI);
921 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
922 LOGP(DNS, LOGL_ERROR,
923 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
924 "from %s\n",
925 nsvci,
926 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
927 nsei, gprs_ns_ll_str(*nsvc));
928 return -EINVAL;
929 }
930
931 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200932 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200933 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
934
935 /* Update the ll info fields */
936 gprs_ns_ll_copy(*nsvc, orig_nsvc);
937 gprs_ns_ll_clear(orig_nsvc);
938 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200939 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
940 /* The incoming RESET_ACK doesn't match the NSEI.
941 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
942 */
943 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
944 NS_PDUT_RESET_ACK,
945 NS_IE_NSEI);
946 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
947 LOGP(DNS, LOGL_ERROR,
948 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
949 nsei, nsvci, gprs_ns_ll_str(*nsvc));
950 return -EINVAL;
951 }
952
953 /* NSEI has changed */
954 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
955 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200956 }
957
958 /* Mark NS-VC as blocked and alive */
959 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
960 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
961 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
962 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
963 /* stop RESET timer */
964 osmo_timer_del(&(*nsvc)->timer);
965 }
966 /* Initiate TEST proc.: Send ALIVE and start timer */
967 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
968 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
969
970 return rc;
971}
972
Harald Welte834f26d2010-05-11 06:20:54 +0200973static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
974{
975 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
976 struct tlv_parsed tp;
977 uint8_t *cause;
978 int rc;
979
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200980 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200981
982 nsvc->state |= NSE_S_BLOCKED;
983
Harald Weltebd33f3d2010-05-30 17:19:38 +0200984 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
985 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200986 if (rc < 0) {
987 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
988 "Error during TLV Parse\n", nsvc->nsei);
989 return rc;
990 }
Harald Welte834f26d2010-05-11 06:20:54 +0200991
992 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
993 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200994 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200995 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200996 return -EINVAL;
997 }
998
999 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1000 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1001
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001002 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001003 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001004
1005 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
1006}
1007
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001008int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1009 struct gprs_nsvc *fallback_nsvc,
1010 struct gprs_nsvc **new_nsvc);
1011
1012int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001013 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001014
Harald Weltea9f23c82011-11-23 15:01:31 +01001015/*! \brief Receive incoming NS message from underlying transport layer
1016 * \param nsi NS instance to which the data belongs
1017 * \param[in] msg message buffer containing newly-received data
1018 * \param[in] saddr socketaddr from which data was received
1019 * \param[in] ll link-layer type in which data was received
1020 * \returns 0 in case of success, < 0 in case of error
1021 *
1022 * This is the main entry point int othe NS imlementation where frames
1023 * from the underlying transport (normally UDP) enter.
1024 */
Harald Weltef030b212010-04-26 19:18:54 +02001025int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001026 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001027{
Harald Weltef030b212010-04-26 19:18:54 +02001028 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001029 int rc = 0;
1030
Harald Weltef030b212010-04-26 19:18:54 +02001031 /* look up the NSVC based on source address */
1032 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001033
Harald Weltef030b212010-04-26 19:18:54 +02001034 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001035 struct gprs_nsvc *fallback_nsvc;
1036
1037 fallback_nsvc = nsi->unknown_nsvc;
1038 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1039 fallback_nsvc->ip.bts_addr = *saddr;
1040 fallback_nsvc->ll = ll;
1041
1042 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1043
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001044 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001045 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001046
1047 rc = 0;
1048 }
1049
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001050 if (nsvc)
1051 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001052
1053 return rc;
1054}
1055
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001056const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001057{
1058 static char buf[80];
1059 snprintf(buf, sizeof(buf), "%s:%u",
1060 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
1061 ntohs(nsvc->ip.bts_addr.sin_port));
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001062 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001063
1064 return buf;
1065}
1066
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001067void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1068{
1069 nsvc->ll = other->ll;
1070
1071 switch (nsvc->ll) {
1072 case GPRS_NS_LL_UDP:
1073 nsvc->ip = other->ip;
1074 break;
1075 case GPRS_NS_LL_FR_GRE:
1076 nsvc->frgre = other->frgre;
1077 break;
1078 default:
1079 break;
1080 }
1081}
1082
1083void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1084{
1085 switch (nsvc->ll) {
1086 case GPRS_NS_LL_UDP:
1087 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1088 nsvc->ip.bts_addr.sin_port = 0;
1089 break;
1090 case GPRS_NS_LL_FR_GRE:
1091 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1092 nsvc->frgre.bts_addr.sin_port = 0;
1093 break;
1094 default:
1095 break;
1096 }
1097}
1098
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001099/*! \brief Create/get NS-VC independently from underlying transport layer
1100 * \param nsi NS instance to which the data belongs
1101 * \param[in] msg message buffer containing newly-received data
1102 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001103 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001104 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1105 * been created or found
1106 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1107 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1108 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1109 * has been created and registered, and GPRS_NS_CS_FOUND if an
1110 * existing NS-VC object has been found with the same NSEI.
1111 *
1112 * This contains the initial NS automaton state (NS-VC not yet attached).
1113 */
1114int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1115 struct gprs_nsvc *fallback_nsvc,
1116 struct gprs_nsvc **new_nsvc)
1117{
1118 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1119 struct gprs_nsvc *existing_nsvc;
1120
1121 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001122 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001123 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001124
1125 int rc;
1126
1127 if (nsh->pdu_type == NS_PDUT_STATUS) {
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001128 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001129 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
Jacob Erlbeck3d557b12013-10-28 13:29:11 +01001135 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
1136 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
1137 LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
1138 "for non-existing NS-VC\n",
1139 gprs_ns_ll_str(fallback_nsvc));
1140 return GPRS_NS_CS_SKIPPED;
1141 }
1142
1143 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
1144 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
1145 LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
1146 "for non-existing NS-VC\n",
1147 gprs_ns_ll_str(fallback_nsvc));
1148 return GPRS_NS_CS_SKIPPED;
1149 }
1150
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001151 /* Only the RESET procedure creates a new NSVC */
1152 if (nsh->pdu_type != NS_PDUT_RESET) {
1153 /* Since we have no NSVC, we have to use a fake */
1154 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1155 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
1156 "from %s for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001157 nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001158 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001159 fallback_nsvc->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001160 fallback_nsvc->state = NSE_S_ALIVE;
1161
1162 rc = gprs_ns_tx_status(fallback_nsvc,
1163 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1164 if (rc < 0) {
1165 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001166 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001167 return rc;
1168 }
1169 return GPRS_NS_CS_REJECTED;
1170 }
1171
1172 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1173 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1174 if (rc < 0) {
1175 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1176 "TLV Parse\n", rc);
1177 return rc;
1178 }
1179 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1180 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1181 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001182 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001183 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001184 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001185 return -EINVAL;
1186 }
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001187 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001188 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001189 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001190 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001191 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001192 if (!existing_nsvc) {
1193 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001194 (*new_nsvc)->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001195 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001196 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001197 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001198 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001199
1200 return GPRS_NS_CS_CREATED;
1201 }
1202
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001203 /* Check NSEI */
1204 if (existing_nsvc->nsei != nsei) {
1205 LOGP(DNS, LOGL_NOTICE,
1206 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1207 nsvci, existing_nsvc->nsei, nsei);
1208
1209 /* Override old NSEI */
1210 existing_nsvc->nsei = nsei;
1211
1212 /* Do statistics */
1213 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1214 }
1215
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001216 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001217 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001218 return GPRS_NS_CS_FOUND;
1219}
1220
1221/*! \brief Process NS message independently from underlying transport layer
1222 * \param nsi NS instance to which the data belongs
1223 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001224 * \param[inout] nsvc refers to the virtual connection, may be modified when
1225 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001226 * \returns 0 in case of success, < 0 in case of error
1227 *
1228 * This contains the main NS automaton.
1229 */
1230int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001231 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001232{
1233 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1234 int rc = 0;
1235
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001236 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001237
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001238 log_set_context(GPRS_CTX_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001239
Harald Welte144e0292010-05-13 11:45:07 +02001240 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001241 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1242 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001243
Harald Welte9ba50052010-03-14 15:45:01 +08001244 switch (nsh->pdu_type) {
1245 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001246 /* If we're dead and blocked and suddenly receive a
1247 * NS-ALIVE out of the blue, we might have been re-started
1248 * and should send a NS-RESET to make sure everything recovers
1249 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001250 if ((*nsvc)->state == NSE_S_BLOCKED)
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001251 rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
1252 else if (!((*nsvc)->state & NSE_S_RESET))
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001253 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001254 break;
1255 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +02001256 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001257 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1258 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001259 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001260 if ((*nsvc)->state & NSE_S_BLOCKED)
1261 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001262 }
Harald Welte9ba50052010-03-14 15:45:01 +08001263 break;
1264 case NS_PDUT_UNITDATA:
1265 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001266 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001267 break;
1268 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001269 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001270 break;
1271 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001272 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001273 break;
1274 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001275 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001276 break;
1277 case NS_PDUT_UNBLOCK:
1278 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001279 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
1280 (*nsvc)->state &= ~NSE_S_BLOCKED;
1281 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1282 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001283 break;
1284 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001285 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001286 /* mark NS-VC as unblocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001287 (*nsvc)->state = NSE_S_ALIVE;
1288 (*nsvc)->remote_state = NSE_S_ALIVE;
1289 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001290 break;
1291 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001292 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001293 break;
1294 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001295 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001296 /* mark remote NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001297 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001298 break;
1299 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001300 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001301 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001302 rc = -EINVAL;
1303 break;
1304 }
1305 return rc;
1306}
1307
Harald Weltea9f23c82011-11-23 15:01:31 +01001308/*! \brief Create a new GPRS NS instance
1309 * \param[in] cb Call-back function for incoming BSSGP data
1310 * \returns dynamically allocated gprs_ns_inst
1311 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001312struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001313{
Harald Welte4fcdd762012-06-16 16:40:42 +08001314 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001315
1316 nsi->cb = cb;
1317 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001318 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1319 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1320 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1321 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1322 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1323 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1324 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001325
Harald Welte60cc6ac2010-05-13 14:20:56 +02001326 /* Create the dummy NSVC that we use for sending
1327 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001328 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Jacob Erlbeck9b591b72013-11-11 09:43:05 +01001329 nsi->unknown_nsvc->nsvci_is_valid = 0;
Harald Welte60cc6ac2010-05-13 14:20:56 +02001330 llist_del(&nsi->unknown_nsvc->list);
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001331 INIT_LLIST_HEAD(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001332
Harald Welte3771d092010-04-30 20:26:32 +02001333 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001334}
1335
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001336void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001337{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001338 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001339
Holger Hans Peter Freyther777b0562014-07-07 20:00:35 +02001340 gprs_nsvc_delete(nsi->unknown_nsvc);
1341
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001342 /* delete all NSVCs and clear their timers */
1343 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1344 gprs_nsvc_delete(nsvc);
1345
1346 /* close socket and unregister */
1347 if (nsi->nsip.fd.data) {
1348 close(nsi->nsip.fd.fd);
1349 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001350 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001351 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001352}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001353
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001354/*! \brief Destroy an entire NS instance
1355 * \param nsi gprs_ns_inst that is to be destroyed
1356 *
1357 * This function releases all resources associated with the
1358 * NS-instance.
1359 */
1360void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1361{
1362 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001363 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001364 talloc_free(nsi);
1365}
1366
1367
1368/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1369 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1370
1371/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001372static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001373 struct sockaddr_in *saddr)
1374{
Harald Welteba4c6662010-05-19 15:38:10 +02001375 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001376 int ret = 0;
1377 socklen_t saddr_len = sizeof(*saddr);
1378
1379 if (!msg) {
1380 *error = -ENOMEM;
1381 return NULL;
1382 }
1383
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001384 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001385 (struct sockaddr *)saddr, &saddr_len);
1386 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001387 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001388 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001389 msgb_free(msg);
1390 *error = ret;
1391 return NULL;
1392 } else if (ret == 0) {
1393 msgb_free(msg);
1394 *error = ret;
1395 return NULL;
1396 }
1397
1398 msg->l2h = msg->data;
1399 msgb_put(msg, ret);
1400
1401 return msg;
1402}
1403
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001404static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001405{
1406 int error;
1407 struct sockaddr_in saddr;
1408 struct gprs_ns_inst *nsi = bfd->data;
1409 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1410
1411 if (!msg)
1412 return error;
1413
Harald Welteb3ee2652010-05-19 14:38:50 +02001414 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001415
1416 msgb_free(msg);
1417
1418 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001419}
1420
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001421static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001422{
1423 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1424 return -EIO;
1425}
1426
Harald Welteb3ee2652010-05-19 14:38:50 +02001427static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001428{
1429 int rc;
1430 struct gprs_ns_inst *nsi = nsvc->nsi;
1431 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1432
1433 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1434 (struct sockaddr *)daddr, sizeof(*daddr));
1435
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001436 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001437
1438 return rc;
1439}
1440
1441/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001442static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001443{
1444 int rc = 0;
1445
1446 if (what & BSC_FD_READ)
1447 rc = handle_nsip_read(bfd);
1448 if (what & BSC_FD_WRITE)
1449 rc = handle_nsip_write(bfd);
1450
1451 return rc;
1452}
1453
Harald Weltea9f23c82011-11-23 15:01:31 +01001454/*! \brief Create a listening socket for GPRS NS/UDP/IP
1455 * \param[in] nsi NS protocol instance to listen
1456 * \returns >=0 (fd) in case of success, negative in case of error
1457 *
1458 * A call to this function will create a UDP socket bound to the port
1459 * number and IP address specified in the NS protocol instance. The
1460 * file descriptor of the socket will be stored in nsi->nsip.fd.
1461 */
Harald Welte7fb05232010-05-19 15:09:09 +02001462int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001463{
Harald Welte73952e32012-06-16 14:59:56 +08001464 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001465 int ret;
1466
Harald Welte73952e32012-06-16 14:59:56 +08001467 in.s_addr = htonl(nsi->nsip.local_ip);
1468
1469 nsi->nsip.fd.cb = nsip_fd_cb;
1470 nsi->nsip.fd.data = nsi;
1471 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1472 IPPROTO_UDP, inet_ntoa(in),
1473 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001474 if (ret < 0)
1475 return ret;
1476
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001477 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1478 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1479 if (ret < 0)
1480 LOGP(DNS, LOGL_ERROR,
1481 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1482 nsi->nsip.dscp, ret, errno);
1483
Harald Weltef030b212010-04-26 19:18:54 +02001484
1485 return ret;
1486}
Harald Welte3771d092010-04-30 20:26:32 +02001487
Harald Weltea9f23c82011-11-23 15:01:31 +01001488/*! \brief Initiate a RESET procedure
1489 * \param[in] nsvc NS-VC in which to start the procedure
1490 * \param[in] cause Numeric NS cause value
1491 *
1492 * This is a high-level function initiating a NS-RESET procedure. It
1493 * will not only send a NS-RESET, but also set the state to BLOCKED and
1494 * start the Tns-reset timer.
1495 */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001496int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001497{
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001498 int rc;
1499
Harald Welteb1020d52010-05-25 22:17:30 +02001500 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1501 nsvc->nsei);
1502
Harald Welte731d1fc2010-05-14 11:53:08 +00001503 /* Mark NS-VC locally as blocked and dead */
Jacob Erlbeck6ac70a42014-10-07 14:12:30 +02001504 nsvc->state = NSE_S_BLOCKED | NSE_S_RESET;
1505
Harald Welte731d1fc2010-05-14 11:53:08 +00001506 /* Send NS-RESET PDU */
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001507 rc = gprs_ns_tx_reset(nsvc, cause);
1508 if (rc < 0) {
Harald Welte731d1fc2010-05-14 11:53:08 +00001509 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1510 nsvc->nsei);
1511 }
1512 /* Start Tns-reset */
1513 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Jacob Erlbeck0540d832014-10-08 11:47:36 +02001514
1515 return rc;
Harald Welte731d1fc2010-05-14 11:53:08 +00001516}
1517
Harald Weltea9f23c82011-11-23 15:01:31 +01001518/*! \brief Establish a NS connection (from the BSS) to the SGSN
1519 * \param nsi NS-instance
1520 * \param[in] dest Destination IP/Port
1521 * \param[in] nsei NSEI of the to-be-established NS-VC
1522 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1523 * \returns struct gprs_nsvc representing the new NS-VC
1524 *
1525 * This function will establish a single NS/UDP/IP connection in uplink
1526 * (BSS to SGSN) direction.
1527 */
Harald Weltef5430362012-06-17 12:25:53 +08001528struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001529 struct sockaddr_in *dest, uint16_t nsei,
1530 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001531{
1532 struct gprs_nsvc *nsvc;
1533
1534 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001535 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001536 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001537 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001538 nsvc->nsei = nsei;
Harald Welte3771d092010-04-30 20:26:32 +02001539 nsvc->remote_end_is_sgsn = 1;
1540
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001541 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1542 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001543}
Harald Weltea9f23c82011-11-23 15:01:31 +01001544
Harald Weltecca49632012-06-16 17:45:59 +08001545void gprs_ns_set_log_ss(int ss)
1546{
1547 DNS = ss;
1548}
1549
Harald Weltea9f23c82011-11-23 15:01:31 +01001550/*! }@ */