blob: 0e9156a32258cc5e9810c288a49f9ffd1a77a4c3 [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
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;
203 /* before RESET procedure: BLOCKED and DEAD */
204 nsvc->state = NSE_S_BLOCKED;
205 nsvc->nsi = nsi;
Harald Welte69a4cf22010-05-03 20:55:10 +0200206 nsvc->timer.cb = gprs_ns_timer_cb;
207 nsvc->timer.data = nsvc;
Harald Welte144e0292010-05-13 11:45:07 +0200208 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci);
Harald Welte69a4cf22010-05-03 20:55:10 +0200209
Harald Weltef030b212010-04-26 19:18:54 +0200210 llist_add(&nsvc->list, &nsi->gprs_nsvcs);
211
212 return nsvc;
213}
Harald Welte9ba50052010-03-14 15:45:01 +0800214
Harald Weltea9f23c82011-11-23 15:01:31 +0100215/*! \brief Delete given NS-VC
216 * \param[in] nsvc gprs_nsvc to be deleted
217 */
Harald Weltef5430362012-06-17 12:25:53 +0800218void gprs_nsvc_delete(struct gprs_nsvc *nsvc)
Harald Welte2bffac52010-05-12 15:55:23 +0000219{
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200220 if (osmo_timer_pending(&nsvc->timer))
221 osmo_timer_del(&nsvc->timer);
Harald Welte2bffac52010-05-12 15:55:23 +0000222 llist_del(&nsvc->list);
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200223 rate_ctr_group_free(nsvc->ctrg);
Harald Welte2bffac52010-05-12 15:55:23 +0000224 talloc_free(nsvc);
225}
226
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200227static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal,
Harald Welte834f26d2010-05-11 06:20:54 +0200228 uint8_t cause)
229{
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200230 struct ns_signal_data nssd = {0};
Harald Welte834f26d2010-05-11 06:20:54 +0200231
232 nssd.nsvc = nsvc;
233 nssd.cause = cause;
234
Harald Welte4fcdd762012-06-16 16:40:42 +0800235 osmo_signal_dispatch(SS_L_NS, signal, &nssd);
Harald Welte834f26d2010-05-11 06:20:54 +0200236}
237
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200238static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc,
239 struct msgb *msg,
240 uint8_t pdu_type, uint8_t ie_type)
241{
242 struct ns_signal_data nssd = {0};
243
244 nssd.nsvc = nsvc;
245 nssd.pdu_type = pdu_type;
246 nssd.ie_type = ie_type;
247 nssd.msg = msg;
248
249 osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd);
250}
251
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200252static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc)
253{
254 struct ns_signal_data nssd = {0};
255
256 nssd.nsvc = nsvc;
257 nssd.old_nsvc = old_nsvc;
258
259 osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd);
260}
261
Harald Welte9ba50052010-03-14 15:45:01 +0800262/* Section 10.3.2, Table 13 */
Harald Welte2577d412010-05-02 09:37:45 +0200263static const struct value_string ns_cause_str[] = {
264 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
265 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
266 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
267 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
268 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
269 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
270 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
271 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
Harald Welte69a4cf22010-05-03 20:55:10 +0200272 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte2577d412010-05-02 09:37:45 +0200273 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
274 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
275 { 0, NULL }
Harald Welte9ba50052010-03-14 15:45:01 +0800276};
277
Harald Weltea9f23c82011-11-23 15:01:31 +0100278/*! \brief Obtain a human-readable string for NS cause value */
Harald Welte2577d412010-05-02 09:37:45 +0200279const char *gprs_ns_cause_str(enum ns_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +0800280{
Harald Welte2577d412010-05-02 09:37:45 +0200281 return get_value_string(ns_cause_str, cause);
Harald Welte9ba50052010-03-14 15:45:01 +0800282}
283
Harald Welte24a655f2010-04-30 19:54:29 +0200284static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Welteb3ee2652010-05-19 14:38:50 +0200285extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200286
Harald Welte24a655f2010-04-30 19:54:29 +0200287static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800288{
Harald Weltef030b212010-04-26 19:18:54 +0200289 int ret;
290
Harald Weltecca49632012-06-16 17:45:59 +0800291 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200292
Harald Welte144e0292010-05-13 11:45:07 +0200293 /* Increment number of Uplink bytes */
Harald Weltec51c23c2010-05-13 12:55:20 +0200294 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]);
295 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +0200296
Harald Welteb3ee2652010-05-19 14:38:50 +0200297 switch (nsvc->ll) {
Harald Weltef030b212010-04-26 19:18:54 +0200298 case GPRS_NS_LL_UDP:
Harald Welte24a655f2010-04-30 19:54:29 +0200299 ret = nsip_sendmsg(nsvc, msg);
Harald Weltef030b212010-04-26 19:18:54 +0200300 break;
Harald Welteb3ee2652010-05-19 14:38:50 +0200301 case GPRS_NS_LL_FR_GRE:
302 ret = gprs_ns_frgre_sendmsg(nsvc, msg);
303 break;
Harald Weltef030b212010-04-26 19:18:54 +0200304 default:
Harald Welteb3ee2652010-05-19 14:38:50 +0200305 LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll);
Harald Weltef030b212010-04-26 19:18:54 +0200306 msgb_free(msg);
307 ret = -EIO;
308 break;
309 }
310 return ret;
Harald Welte9ba50052010-03-14 15:45:01 +0800311}
312
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200313static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type)
Harald Welte9ba50052010-03-14 15:45:01 +0800314{
Harald Welteba4c6662010-05-19 15:38:10 +0200315 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800316 struct gprs_ns_hdr *nsh;
317
Harald Weltecca49632012-06-16 17:45:59 +0800318 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200319
Harald Welte9ba50052010-03-14 15:45:01 +0800320 if (!msg)
321 return -ENOMEM;
322
Harald Welte144e0292010-05-13 11:45:07 +0200323 msg->l2h = msgb_put(msg, sizeof(*nsh));
324 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800325
326 nsh->pdu_type = pdu_type;
327
Harald Welte24a655f2010-04-30 19:54:29 +0200328 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800329}
330
Harald Weltea9f23c82011-11-23 15:01:31 +0100331/*! \brief Transmit a NS-RESET on a given NSVC
332 * \param[in] nsvc NS-VC used for transmission
333 * \paam[in] cause Numeric NS cause value
334 */
Harald Welte834f26d2010-05-11 06:20:54 +0200335int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte69a4cf22010-05-03 20:55:10 +0200336{
Harald Welteba4c6662010-05-19 15:38:10 +0200337 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte69a4cf22010-05-03 20:55:10 +0200338 struct gprs_ns_hdr *nsh;
Harald Welte69a4cf22010-05-03 20:55:10 +0200339 uint16_t nsvci = htons(nsvc->nsvci);
340 uint16_t nsei = htons(nsvc->nsei);
341
Harald Weltecca49632012-06-16 17:45:59 +0800342 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200343
Harald Welte69a4cf22010-05-03 20:55:10 +0200344 if (!msg)
345 return -ENOMEM;
346
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000347 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n",
348 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
349
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200350 nsvc->state |= NSE_S_RESET;
351
Harald Welte144e0292010-05-13 11:45:07 +0200352 msg->l2h = msgb_put(msg, sizeof(*nsh));
353 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte69a4cf22010-05-03 20:55:10 +0200354 nsh->pdu_type = NS_PDUT_RESET;
355
356 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
357 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
358 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
359
360 return gprs_ns_tx(nsvc, msg);
361
362}
363
Harald Weltea9f23c82011-11-23 15:01:31 +0100364/*! \brief Transmit a NS-STATUS on a given NSVC
365 * \param[in] nsvc NS-VC to be used for transmission
366 * \param[in] cause Numeric NS cause value
367 * \param[in] bvci BVCI to be reset within NSVC
368 * \param[in] orig_msg message causing the STATUS */
Harald Weltec1402a62010-05-12 11:48:44 +0200369int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause,
370 uint16_t bvci, struct msgb *orig_msg)
371{
Harald Welteba4c6662010-05-19 15:38:10 +0200372 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltec1402a62010-05-12 11:48:44 +0200373 struct gprs_ns_hdr *nsh;
374 uint16_t nsvci = htons(nsvc->nsvci);
375
Harald Weltecca49632012-06-16 17:45:59 +0800376 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200377
Harald Weltec1402a62010-05-12 11:48:44 +0200378 bvci = htons(bvci);
379
380 if (!msg)
381 return -ENOMEM;
382
Harald Welte90af1942010-05-15 23:02:24 +0200383 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n",
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000384 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
385
Harald Welte144e0292010-05-13 11:45:07 +0200386 msg->l2h = msgb_put(msg, sizeof(*nsh));
387 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Weltec1402a62010-05-12 11:48:44 +0200388 nsh->pdu_type = NS_PDUT_STATUS;
389
390 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
391
392 /* Section 9.2.7.1: Static conditions for NS-VCI */
393 if (cause == NS_CAUSE_NSVC_BLOCKED ||
394 cause == NS_CAUSE_NSVC_UNKNOWN)
395 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
396
397 /* Section 9.2.7.2: Static conditions for NS PDU */
398 switch (cause) {
399 case NS_CAUSE_SEM_INCORR_PDU:
400 case NS_CAUSE_PDU_INCOMP_PSTATE:
401 case NS_CAUSE_PROTO_ERR_UNSPEC:
402 case NS_CAUSE_INVAL_ESSENT_IE:
403 case NS_CAUSE_MISSING_ESSENT_IE:
404 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
405 orig_msg->l2h);
406 break;
407 default:
408 break;
409 }
410
411 /* Section 9.2.7.3: Static conditions for BVCI */
412 if (cause == NS_CAUSE_BVCI_UNKNOWN)
413 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
414
415 return gprs_ns_tx(nsvc, msg);
416}
417
Harald Weltea9f23c82011-11-23 15:01:31 +0100418/*! \brief Transmit a NS-BLOCK on a tiven NS-VC
419 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
420 * \param[in] cause Numeric NS Cause value
421 * \returns 0 in case of success
422 */
Harald Welte834f26d2010-05-11 06:20:54 +0200423int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause)
424{
Harald Welteba4c6662010-05-19 15:38:10 +0200425 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte834f26d2010-05-11 06:20:54 +0200426 struct gprs_ns_hdr *nsh;
427 uint16_t nsvci = htons(nsvc->nsvci);
428
Harald Weltecca49632012-06-16 17:45:59 +0800429 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +0200430
Harald Welte834f26d2010-05-11 06:20:54 +0200431 if (!msg)
432 return -ENOMEM;
433
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000434 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n",
435 nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause));
436
Harald Welte834f26d2010-05-11 06:20:54 +0200437 /* be conservative and mark it as blocked even now! */
438 nsvc->state |= NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200439 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +0200440
Harald Welte144e0292010-05-13 11:45:07 +0200441 msg->l2h = msgb_put(msg, sizeof(*nsh));
442 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte834f26d2010-05-11 06:20:54 +0200443 nsh->pdu_type = NS_PDUT_BLOCK;
444
445 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
446 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
447
448 return gprs_ns_tx(nsvc, msg);
449}
450
Harald Weltea9f23c82011-11-23 15:01:31 +0100451/*! \brief Transmit a NS-UNBLOCK on a given NS-VC
452 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
453 * \returns 0 in case of success
454 */
Harald Welte834f26d2010-05-11 06:20:54 +0200455int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc)
456{
Harald Weltecca49632012-06-16 17:45:59 +0800457 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte6ecaa3d2010-05-12 12:01:33 +0000458 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n",
459 nsvc->nsei, nsvc->nsvci);
460
Harald Welte834f26d2010-05-11 06:20:54 +0200461 return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK);
462}
463
Harald Weltea9f23c82011-11-23 15:01:31 +0100464/*! \brief Transmit a NS-ALIVE on a given NS-VC
465 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
466 * \returns 0 in case of success
467 */
Harald Welteb983c312010-05-12 12:11:45 +0000468int gprs_ns_tx_alive(struct gprs_nsvc *nsvc)
469{
Harald Weltecca49632012-06-16 17:45:59 +0800470 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000471 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n",
472 nsvc->nsei, nsvc->nsvci);
473
474 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE);
475}
476
Harald Weltea9f23c82011-11-23 15:01:31 +0100477/*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC
478 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
479 * \returns 0 in case of success
480 */
Harald Welteb983c312010-05-12 12:11:45 +0000481int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc)
482{
Harald Weltecca49632012-06-16 17:45:59 +0800483 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welteb983c312010-05-12 12:11:45 +0000484 LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n",
485 nsvc->nsei, nsvc->nsvci);
486
487 return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
488}
489
Harald Weltefe4ab902010-05-12 17:19:53 +0000490static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = {
491 [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET,
492 [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE,
493 [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST,
Harald Welte69a4cf22010-05-03 20:55:10 +0200494};
495
Harald Welte7c24b9e2010-05-12 12:21:52 +0000496static const struct value_string timer_mode_strs[] = {
497 { NSVC_TIMER_TNS_RESET, "tns-reset" },
498 { NSVC_TIMER_TNS_ALIVE, "tns-alive" },
499 { NSVC_TIMER_TNS_TEST, "tns-test" },
500 { 0, NULL }
501};
502
Harald Welte69a4cf22010-05-03 20:55:10 +0200503static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode)
504{
Harald Weltefe4ab902010-05-12 17:19:53 +0000505 enum ns_timeout tout = timer_mode_tout[mode];
506 unsigned int seconds = nsvc->nsi->timeout[tout];
507
Harald Weltecca49632012-06-16 17:45:59 +0800508 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000509 DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n",
510 nsvc->nsei, get_value_string(timer_mode_strs, mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000511 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000512
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200513 if (osmo_timer_pending(&nsvc->timer))
514 osmo_timer_del(&nsvc->timer);
Harald Welte69a4cf22010-05-03 20:55:10 +0200515
516 nsvc->timer_mode = mode;
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200517 osmo_timer_schedule(&nsvc->timer, seconds, 0);
Harald Welte69a4cf22010-05-03 20:55:10 +0200518}
519
Harald Welte80405452010-05-03 20:16:13 +0200520static void gprs_ns_timer_cb(void *data)
Harald Welte9ba50052010-03-14 15:45:01 +0800521{
522 struct gprs_nsvc *nsvc = data;
Harald Weltefe4ab902010-05-12 17:19:53 +0000523 enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode];
524 unsigned int seconds = nsvc->nsi->timeout[tout];
Harald Welte9ba50052010-03-14 15:45:01 +0800525
Harald Weltecca49632012-06-16 17:45:59 +0800526 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte4e187c62010-05-12 13:55:36 +0000527 DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n",
528 nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode),
Harald Weltefe4ab902010-05-12 17:19:53 +0000529 seconds);
Harald Welte7c24b9e2010-05-12 12:21:52 +0000530
Harald Welte80405452010-05-03 20:16:13 +0200531 switch (nsvc->timer_mode) {
532 case NSVC_TIMER_TNS_ALIVE:
Harald Welte9ba50052010-03-14 15:45:01 +0800533 /* Tns-alive case: we expired without response ! */
534 nsvc->alive_retries++;
Harald Weltefe4ab902010-05-12 17:19:53 +0000535 if (nsvc->alive_retries >
536 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) {
Harald Welte9ba50052010-03-14 15:45:01 +0800537 /* mark as dead and blocked */
538 nsvc->state = NSE_S_BLOCKED;
Harald Weltec51c23c2010-05-13 12:55:20 +0200539 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
540 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]);
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200541 LOGP(DNS, LOGL_NOTICE,
542 "NSEI=%u Tns-alive expired more then "
Harald Welte69a4cf22010-05-03 20:55:10 +0200543 "%u times, blocking NS-VC\n", nsvc->nsei,
Harald Weltefe4ab902010-05-12 17:19:53 +0000544 nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]);
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +0200545 ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0);
546 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED);
Harald Welte9ba50052010-03-14 15:45:01 +0800547 return;
548 }
Harald Welteb983c312010-05-12 12:11:45 +0000549 /* Tns-test case: send NS-ALIVE PDU */
550 gprs_ns_tx_alive(nsvc);
551 /* start Tns-alive timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200552 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
Harald Welte80405452010-05-03 20:16:13 +0200553 break;
554 case NSVC_TIMER_TNS_TEST:
Harald Welte9ba50052010-03-14 15:45:01 +0800555 /* Tns-test case: send NS-ALIVE PDU */
Harald Welteb983c312010-05-12 12:11:45 +0000556 gprs_ns_tx_alive(nsvc);
557 /* start Tns-alive timer (transition into faster
558 * alive retransmissions) */
Harald Welte7c24b9e2010-05-12 12:21:52 +0000559 nsvc->alive_retries = 0;
Harald Welte69a4cf22010-05-03 20:55:10 +0200560 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE);
561 break;
562 case NSVC_TIMER_TNS_RESET:
563 /* Chapter 7.3: Re-send the RESET */
Harald Welte570fb832010-05-03 21:05:24 +0200564 gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Harald Weltec1402a62010-05-12 11:48:44 +0200565 /* Re-start Tns-reset timer */
Harald Welte69a4cf22010-05-03 20:55:10 +0200566 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte80405452010-05-03 20:16:13 +0200567 break;
Harald Welte2fc725d2010-05-11 10:15:26 +0200568 case _NSVC_TIMER_NR:
569 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800570 }
Harald Welte9ba50052010-03-14 15:45:01 +0800571}
572
573/* Section 9.2.6 */
Harald Weltec5478482010-03-18 00:01:43 +0800574static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc)
Harald Welte9ba50052010-03-14 15:45:01 +0800575{
Harald Welteba4c6662010-05-19 15:38:10 +0200576 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Welte9ba50052010-03-14 15:45:01 +0800577 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200578 uint16_t nsvci, nsei;
Harald Welte9ba50052010-03-14 15:45:01 +0800579
Harald Weltecca49632012-06-16 17:45:59 +0800580 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800581 if (!msg)
582 return -ENOMEM;
583
Harald Weltec5478482010-03-18 00:01:43 +0800584 nsvci = htons(nsvc->nsvci);
585 nsei = htons(nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800586
Harald Welte144e0292010-05-13 11:45:07 +0200587 msg->l2h = msgb_put(msg, sizeof(*nsh));
588 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800589
590 nsh->pdu_type = NS_PDUT_RESET_ACK;
591
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200592 LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +0200593 nsvc->nsei, nsvc->nsvci);
Harald Weltec5478482010-03-18 00:01:43 +0800594
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200595 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
596 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800597
Harald Welte24a655f2010-04-30 19:54:29 +0200598 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800599}
600
Harald Weltea9f23c82011-11-23 15:01:31 +0100601/*! \brief High-level function for transmitting a NS-UNITDATA messsage
602 * \param[in] nsi NS-instance on which we shall transmit
603 * \param[in] msg struct msgb to be trasnmitted
604 *
605 * This function obtains the NS-VC by the msgb_nsei(msg) and then checks
606 * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS
607 * header for the NS-UNITDATA message type and sends it off.
608 *
609 * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive
610 */
Harald Welte24a655f2010-04-30 19:54:29 +0200611int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800612{
Harald Welte24a655f2010-04-30 19:54:29 +0200613 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +0800614 struct gprs_ns_hdr *nsh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200615 uint16_t bvci = msgb_bvci(msg);
Harald Welte24a655f2010-04-30 19:54:29 +0200616
Jacob Erlbeck69017152013-10-14 12:03:54 +0200617 nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg));
Harald Welte24a655f2010-04-30 19:54:29 +0200618 if (!nsvc) {
Jacob Erlbeck69017152013-10-14 12:03:54 +0200619 int rc;
620 if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) {
621 LOGP(DNS, LOGL_ERROR,
622 "All NS-VCs for NSEI %u are either dead or blocked!\n",
623 msgb_nsei(msg));
624 rc = -EBUSY;
625 } else {
626 LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u "
627 "to NS-VC!\n", msgb_nsei(msg));
628 rc = -EINVAL;
629 }
630
Harald Weltef47df642010-08-09 21:15:40 +0800631 msgb_free(msg);
Jacob Erlbeck69017152013-10-14 12:03:54 +0200632 return rc;
Harald Welte24a655f2010-04-30 19:54:29 +0200633 }
Harald Weltecca49632012-06-16 17:45:59 +0800634 log_set_context(GPRS_CTX_NSVC, nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +0800635
Harald Welteec20ba42010-05-13 12:18:49 +0200636 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
637 nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800638 if (!nsh) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200639 LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n");
Harald Weltef47df642010-08-09 21:15:40 +0800640 msgb_free(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800641 return -EIO;
642 }
643
644 nsh->pdu_type = NS_PDUT_UNITDATA;
645 /* spare octet in data[0] */
646 nsh->data[1] = bvci >> 8;
647 nsh->data[2] = bvci & 0xff;
648
Harald Welte24a655f2010-04-30 19:54:29 +0200649 return gprs_ns_tx(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800650}
651
652/* Section 9.2.10: receive side */
Harald Welte24a655f2010-04-30 19:54:29 +0200653static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800654{
655 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200656 uint16_t bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800657
Harald Weltec1402a62010-05-12 11:48:44 +0200658 if (nsvc->state & NSE_S_BLOCKED)
659 return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED,
660 0, msg);
661
Harald Welte9ba50052010-03-14 15:45:01 +0800662 /* spare octet in data[0] */
663 bvci = nsh->data[1] << 8 | nsh->data[2];
Harald Welteec19c102010-05-02 09:50:42 +0200664 msgb_bssgph(msg) = &nsh->data[3];
Harald Welte30bc19a2010-05-02 11:19:37 +0200665 msgb_bvci(msg) = bvci;
Harald Welte9ba50052010-03-14 15:45:01 +0800666
667 /* call upper layer (BSSGP) */
Harald Weltef030b212010-04-26 19:18:54 +0200668 return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800669}
670
671/* Section 9.2.7 */
Harald Welte24a655f2010-04-30 19:54:29 +0200672static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800673{
Harald Weltef030b212010-04-26 19:18:54 +0200674 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800675 struct tlv_parsed tp;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200676 uint8_t cause;
Harald Welte9ba50052010-03-14 15:45:01 +0800677 int rc;
678
Harald Welte41337832010-05-16 00:24:26 +0200679 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800680
Harald Weltebd33f3d2010-05-30 17:19:38 +0200681 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
682 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200683 if (rc < 0) {
684 LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n");
685 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: "
686 "Error during TLV Parse\n", nsvc->nsei);
687 return rc;
688 }
Harald Welte9ba50052010-03-14 15:45:01 +0800689
690 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) {
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200691 LOGPC(DNS, LOGL_INFO, "missing cause IE\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800692 return -EINVAL;
693 }
694
695 cause = *TLVP_VAL(&tp, NS_IE_CAUSE);
Harald Welte41337832010-05-16 00:24:26 +0200696 LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause));
Harald Welte9ba50052010-03-14 15:45:01 +0800697
698 return 0;
699}
700
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200701/* Replace a nsvc object with another based on NSVCI.
702 * This function replaces looks for a NSVC with the given NSVCI and replaces it
703 * if possible and necessary. If replaced, the former value of *nsvc is
704 * returned in *old_nsvc.
705 * \return != 0 if *nsvc points to a matching NSVC.
706 */
707static int gprs_nsvc_replace_if_found(uint16_t nsvci,
708 struct gprs_nsvc **nsvc,
709 struct gprs_nsvc **old_nsvc)
710{
711 struct gprs_nsvc *matching_nsvc;
712
713 if ((*nsvc)->nsvci == nsvci) {
714 *old_nsvc = NULL;
715 return 1;
716 }
717
718 matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci);
719
720 if (!matching_nsvc)
721 return 0;
722
723 /* The NS-VCI is already used by this NS-VC */
724
725 char *old_peer;
726
727 /* Exchange the NS-VC objects */
728 *old_nsvc = *nsvc;
729 *nsvc = matching_nsvc;
730
731 /* Do logging */
732 old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc));
733 LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n",
734 nsvci, old_peer, gprs_ns_ll_str(*nsvc));
735
736 talloc_free(old_peer);
737
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200738 return 1;
739}
740
Harald Welte9ba50052010-03-14 15:45:01 +0800741/* Section 7.3 */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200742static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800743{
744 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Harald Welte9ba50052010-03-14 15:45:01 +0800745 struct tlv_parsed tp;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200746 uint8_t cause;
747 uint16_t nsvci, nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200748 struct gprs_nsvc *orig_nsvc = NULL;
Harald Welte9ba50052010-03-14 15:45:01 +0800749 int rc;
750
Harald Weltebd33f3d2010-05-30 17:19:38 +0200751 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
752 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200753 if (rc < 0) {
754 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET "
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200755 "Error during TLV Parse\n", (*nsvc)->nsei);
Harald Welte36250382010-05-28 10:08:14 +0200756 return rc;
757 }
Harald Welte9ba50052010-03-14 15:45:01 +0800758
759 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
760 !TLVP_PRESENT(&tp, NS_IE_VCI) ||
761 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200762 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200763 gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800764 return -EINVAL;
765 }
766
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200767 cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
768 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
769 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Harald Welte9ba50052010-03-14 15:45:01 +0800770
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200771 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n",
772 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
773 nsei, nsvci, gprs_ns_cause_str(cause));
774
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200775 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) {
776 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200777 /* The incoming RESET doesn't match the NSVCI. Send an
778 * appropriate RESET_ACK and ignore the RESET.
779 * See 3GPP TS 08.16, 7.3.1, 2nd paragraph.
780 */
781 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
782 NS_PDUT_RESET,
783 NS_IE_VCI);
784 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
785 gprs_ns_tx_reset_ack(*nsvc);
786 return 0;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200787 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200788
789 /* NS-VCI has changed */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200790 if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) {
791 LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d "
792 "at %s\n",
793 nsvci, (*nsvc)->nsvci,
794 gprs_ns_ll_str(*nsvc));
795 orig_nsvc = *nsvc;
796 *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci);
797 (*nsvc)->nsvci_is_valid = 1;
798 (*nsvc)->nsei = nsei;
799 }
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200800 }
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200801
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200802 if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) {
803 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
804 /* The incoming RESET doesn't match the NSEI. Send an
805 * appropriate RESET_ACK and ignore the RESET.
806 * See 3GPP TS 08.16, 7.3.1, 3rd paragraph.
807 */
808 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
809 NS_PDUT_RESET,
810 NS_IE_NSEI);
811 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
Holger Hans Peter Freyther726e2722013-10-25 11:05:10 +0200812 rc = gprs_ns_tx_reset_ack(*nsvc);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200813 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200814 return 0;
815 }
816
817 /* NSEI has changed */
818 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
819 (*nsvc)->nsei = nsei;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200820 }
Harald Weltebbc9fac2010-05-03 18:54:12 +0200821
Harald Weltec1402a62010-05-12 11:48:44 +0200822 /* Mark NS-VC as blocked and alive */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200823 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Weltee8b9ca22010-05-11 18:18:31 +0200824
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200825 if (orig_nsvc) {
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200826 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200827 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200828
829 /* Update the ll info fields */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200830 gprs_ns_ll_copy(*nsvc, orig_nsvc);
831 gprs_ns_ll_clear(orig_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200832 } else {
833 (*nsvc)->nsei = nsei;
834 (*nsvc)->nsvci = nsvci;
835 (*nsvc)->nsvci_is_valid = 1;
836 }
Harald Welte9ba50052010-03-14 15:45:01 +0800837
Harald Welte834f26d2010-05-11 06:20:54 +0200838 /* inform interested parties about the fact that this NSVC
839 * has received RESET */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200840 ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause);
Harald Welte834f26d2010-05-11 06:20:54 +0200841
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200842 rc = gprs_ns_tx_reset_ack(*nsvc);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200843
844 /* start the test procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200845 gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE);
846 nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST);
Jacob Erlbeck05395a62013-10-08 12:04:45 +0200847
848 return rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800849}
850
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200851static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg)
852{
853 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
854 struct tlv_parsed tp;
855 uint16_t nsvci, nsei;
856 struct gprs_nsvc *orig_nsvc = NULL;
857 int rc;
858
859 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
860 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
861 if (rc < 0) {
862 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK "
863 "Error during TLV Parse\n", (*nsvc)->nsei);
864 return rc;
865 }
866
867 if (!TLVP_PRESENT(&tp, NS_IE_VCI) ||
868 !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
869 LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n");
Holger Hans Peter Freyther7c91bfd2013-10-25 11:02:05 +0200870 rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +0200871 CHECK_TX_RC(rc, *nsvc);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200872 return -EINVAL;
873 }
874
875 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
876 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
877
878 LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n",
879 (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)",
880 nsei, nsvci);
881
882 if (!((*nsvc)->state & NSE_S_RESET)) {
883 /* Not waiting for a RESET_ACK on this NS-VC, ignore it.
884 * See 3GPP TS 08.16, 7.3.1, 5th paragraph.
885 */
886 LOGP(DNS, LOGL_ERROR,
887 "NS RESET ACK Discarding unexpected message for "
888 "NS-VCI %d from SGSN NSEI=%d\n",
889 nsvci, nsei);
890 return 0;
891 }
892
893 if (!(*nsvc)->nsvci_is_valid) {
894 LOGP(DNS, LOGL_NOTICE,
895 "NS RESET ACK Uninitialised NS-VC (%u) for "
896 "NS-VCI %d, NSEI=%d from %s\n",
897 (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc));
898 return -EINVAL;
899 }
900
901 if ((*nsvc)->nsvci != nsvci) {
902 /* NS-VCI has changed */
903
904 /* if !0, use another NSVC object that matches the NSVCI */
905 int use_other_nsvc;
906
907 /* Only do this with BSS peers */
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200908 use_other_nsvc = !(*nsvc)->remote_end_is_sgsn &&
909 !(*nsvc)->persistent;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200910
911 if (use_other_nsvc)
912 /* Update *nsvc to point to the right NSVC object */
913 use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc,
914 &orig_nsvc);
915
916 if (!use_other_nsvc) {
917 /* The incoming RESET_ACK doesn't match the NSVCI.
918 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
919 */
920 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
921 NS_PDUT_RESET_ACK,
922 NS_IE_VCI);
923 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]);
924 LOGP(DNS, LOGL_ERROR,
925 "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) "
926 "from %s\n",
927 nsvci,
928 (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS",
929 nsei, gprs_ns_ll_str(*nsvc));
930 return -EINVAL;
931 }
932
933 /* Notify others */
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200934 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]);
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200935 ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc);
936
937 /* Update the ll info fields */
938 gprs_ns_ll_copy(*nsvc, orig_nsvc);
939 gprs_ns_ll_clear(orig_nsvc);
940 } else if ((*nsvc)->nsei != nsei) {
Jacob Erlbeckab852ee2013-10-24 01:33:24 +0200941 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
942 /* The incoming RESET_ACK doesn't match the NSEI.
943 * See 3GPP TS 08.16, 7.3.1, 4th paragraph.
944 */
945 ns_osmo_signal_dispatch_mismatch(*nsvc, msg,
946 NS_PDUT_RESET_ACK,
947 NS_IE_NSEI);
948 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]);
949 LOGP(DNS, LOGL_ERROR,
950 "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n",
951 nsei, nsvci, gprs_ns_ll_str(*nsvc));
952 return -EINVAL;
953 }
954
955 /* NSEI has changed */
956 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]);
957 (*nsvc)->nsei = nsei;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200958 }
959
960 /* Mark NS-VC as blocked and alive */
961 (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE;
962 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
963 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]);
964 if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) {
965 /* stop RESET timer */
966 osmo_timer_del(&(*nsvc)->timer);
967 }
968 /* Initiate TEST proc.: Send ALIVE and start timer */
969 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE);
970 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
971
972 return rc;
973}
974
Harald Welte834f26d2010-05-11 06:20:54 +0200975static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg)
976{
977 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
978 struct tlv_parsed tp;
979 uint8_t *cause;
980 int rc;
981
Harald Weltee4ecc8c2010-05-12 00:16:57 +0200982 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei);
Harald Welte834f26d2010-05-11 06:20:54 +0200983
984 nsvc->state |= NSE_S_BLOCKED;
985
Harald Weltebd33f3d2010-05-30 17:19:38 +0200986 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
987 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Harald Welte36250382010-05-28 10:08:14 +0200988 if (rc < 0) {
989 LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK "
990 "Error during TLV Parse\n", nsvc->nsei);
991 return rc;
992 }
Harald Welte834f26d2010-05-11 06:20:54 +0200993
994 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
995 !TLVP_PRESENT(&tp, NS_IE_VCI)) {
Harald Welte834f26d2010-05-11 06:20:54 +0200996 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Harald Weltec1402a62010-05-12 11:48:44 +0200997 gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg);
Harald Welte834f26d2010-05-11 06:20:54 +0200998 return -EINVAL;
999 }
1000
1001 cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE);
1002 //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI);
1003
Pablo Neira Ayuso2b88a2a2011-05-06 12:12:31 +02001004 ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause);
Harald Weltec51c23c2010-05-13 12:55:20 +02001005 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]);
Harald Welte834f26d2010-05-11 06:20:54 +02001006
1007 return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK);
1008}
1009
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001010int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1011 struct gprs_nsvc *fallback_nsvc,
1012 struct gprs_nsvc **new_nsvc);
1013
1014int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001015 struct gprs_nsvc **nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001016
Harald Weltea9f23c82011-11-23 15:01:31 +01001017/*! \brief Receive incoming NS message from underlying transport layer
1018 * \param nsi NS instance to which the data belongs
1019 * \param[in] msg message buffer containing newly-received data
1020 * \param[in] saddr socketaddr from which data was received
1021 * \param[in] ll link-layer type in which data was received
1022 * \returns 0 in case of success, < 0 in case of error
1023 *
1024 * This is the main entry point int othe NS imlementation where frames
1025 * from the underlying transport (normally UDP) enter.
1026 */
Harald Weltef030b212010-04-26 19:18:54 +02001027int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
Harald Welteb3ee2652010-05-19 14:38:50 +02001028 struct sockaddr_in *saddr, enum gprs_ns_ll ll)
Harald Welte9ba50052010-03-14 15:45:01 +08001029{
Harald Weltef030b212010-04-26 19:18:54 +02001030 struct gprs_nsvc *nsvc;
Harald Welte9ba50052010-03-14 15:45:01 +08001031 int rc = 0;
1032
Harald Weltef030b212010-04-26 19:18:54 +02001033 /* look up the NSVC based on source address */
1034 nsvc = nsvc_by_rem_addr(nsi, saddr);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001035
Harald Weltef030b212010-04-26 19:18:54 +02001036 if (!nsvc) {
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001037 struct gprs_nsvc *fallback_nsvc;
1038
1039 fallback_nsvc = nsi->unknown_nsvc;
1040 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1041 fallback_nsvc->ip.bts_addr = *saddr;
1042 fallback_nsvc->ll = ll;
1043
1044 rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc);
1045
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001046 if (rc < 0)
Harald Welte36250382010-05-28 10:08:14 +02001047 return rc;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001048
1049 rc = 0;
1050 }
1051
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001052 if (nsvc)
1053 rc = gprs_ns_process_msg(nsi, msg, &nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001054
1055 return rc;
1056}
1057
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001058const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001059{
1060 static char buf[80];
1061 snprintf(buf, sizeof(buf), "%s:%u",
1062 inet_ntoa(nsvc->ip.bts_addr.sin_addr),
1063 ntohs(nsvc->ip.bts_addr.sin_port));
Holger Hans Peter Freyther0cccf402013-10-25 11:00:23 +02001064 buf[sizeof(buf) - 1] = '\0';
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001065
1066 return buf;
1067}
1068
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001069void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other)
1070{
1071 nsvc->ll = other->ll;
1072
1073 switch (nsvc->ll) {
1074 case GPRS_NS_LL_UDP:
1075 nsvc->ip = other->ip;
1076 break;
1077 case GPRS_NS_LL_FR_GRE:
1078 nsvc->frgre = other->frgre;
1079 break;
1080 default:
1081 break;
1082 }
1083}
1084
1085void gprs_ns_ll_clear(struct gprs_nsvc *nsvc)
1086{
1087 switch (nsvc->ll) {
1088 case GPRS_NS_LL_UDP:
1089 nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY;
1090 nsvc->ip.bts_addr.sin_port = 0;
1091 break;
1092 case GPRS_NS_LL_FR_GRE:
1093 nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY;
1094 nsvc->frgre.bts_addr.sin_port = 0;
1095 break;
1096 default:
1097 break;
1098 }
1099}
1100
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001101/*! \brief Create/get NS-VC independently from underlying transport layer
1102 * \param nsi NS instance to which the data belongs
1103 * \param[in] msg message buffer containing newly-received data
1104 * \param[in] fallback_nsvc is used to send error messages back to the peer
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001105 * and to initialise the ll info of a created NS-VC object
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001106 * \param[out] new_nsvc contains a pointer to a NS-VC object if one has
1107 * been created or found
1108 * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been
1109 * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and
1110 * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object
1111 * has been created and registered, and GPRS_NS_CS_FOUND if an
1112 * existing NS-VC object has been found with the same NSEI.
1113 *
1114 * This contains the initial NS automaton state (NS-VC not yet attached).
1115 */
1116int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg,
1117 struct gprs_nsvc *fallback_nsvc,
1118 struct gprs_nsvc **new_nsvc)
1119{
1120 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
1121 struct gprs_nsvc *existing_nsvc;
1122
1123 struct tlv_parsed tp;
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001124 uint16_t nsvci;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001125 uint16_t nsei;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001126
1127 int rc;
1128
1129 if (nsh->pdu_type == NS_PDUT_STATUS) {
1130 LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s "
1131 "for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001132 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001133 return GPRS_NS_CS_SKIPPED;
1134 }
1135
1136 /* Only the RESET procedure creates a new NSVC */
1137 if (nsh->pdu_type != NS_PDUT_RESET) {
1138 /* Since we have no NSVC, we have to use a fake */
1139 log_set_context(GPRS_CTX_NSVC, fallback_nsvc);
1140 LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x "
1141 "from %s for non-existing NS-VC\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001142 nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001143 fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001144 fallback_nsvc->nsvci_is_valid = 0;
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001145 fallback_nsvc->state = NSE_S_ALIVE;
1146
1147 rc = gprs_ns_tx_status(fallback_nsvc,
1148 NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg);
1149 if (rc < 0) {
1150 LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001151 rc, gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001152 return rc;
1153 }
1154 return GPRS_NS_CS_REJECTED;
1155 }
1156
1157 rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data,
1158 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1159 if (rc < 0) {
1160 LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during "
1161 "TLV Parse\n", rc);
1162 return rc;
1163 }
1164 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
1165 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
1166 LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001167 rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0,
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001168 msg);
Holger Hans Peter Freyther9e1cd5a2013-10-25 11:09:26 +02001169 CHECK_TX_RC(rc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001170 return -EINVAL;
1171 }
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001172 nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI));
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001173 nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI));
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001174 /* Check if we already know this NSVCI, the remote end might
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001175 * simply have changed addresses, or it is a SGSN */
Jacob Erlbeckb6390f92013-10-08 12:04:46 +02001176 existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001177 if (!existing_nsvc) {
1178 *new_nsvc = gprs_nsvc_create(nsi, 0xffff);
1179 log_set_context(GPRS_CTX_NSVC, *new_nsvc);
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001180 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001181 LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n",
Jacob Erlbeck96550e02013-10-14 22:06:47 +02001182 gprs_ns_ll_str(fallback_nsvc));
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001183
1184 return GPRS_NS_CS_CREATED;
1185 }
1186
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001187 /* Check NSEI */
1188 if (existing_nsvc->nsei != nsei) {
1189 LOGP(DNS, LOGL_NOTICE,
1190 "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n",
1191 nsvci, existing_nsvc->nsei, nsei);
1192
1193 /* Override old NSEI */
1194 existing_nsvc->nsei = nsei;
1195
1196 /* Do statistics */
1197 rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]);
1198 }
1199
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001200 *new_nsvc = existing_nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001201 gprs_ns_ll_copy(*new_nsvc, fallback_nsvc);
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001202 return GPRS_NS_CS_FOUND;
1203}
1204
1205/*! \brief Process NS message independently from underlying transport layer
1206 * \param nsi NS instance to which the data belongs
1207 * \param[in] msg message buffer containing newly-received data
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001208 * \param[inout] nsvc refers to the virtual connection, may be modified when
1209 * processing a NS_RESET
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001210 * \returns 0 in case of success, < 0 in case of error
1211 *
1212 * This contains the main NS automaton.
1213 */
1214int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg,
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001215 struct gprs_nsvc **nsvc)
Jacob Erlbeck84cdc702013-10-08 12:04:44 +02001216{
1217 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1218 int rc = 0;
1219
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001220 msgb_nsei(msg) = (*nsvc)->nsei;
Harald Weltec5478482010-03-18 00:01:43 +08001221
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001222 log_set_context(GPRS_CTX_NSVC, *nsvc);
Harald Welte91f7f4b2010-05-15 23:52:02 +02001223
Harald Welte144e0292010-05-13 11:45:07 +02001224 /* Increment number of Incoming bytes */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001225 rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]);
1226 rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg));
Harald Welte144e0292010-05-13 11:45:07 +02001227
Harald Welte9ba50052010-03-14 15:45:01 +08001228 switch (nsh->pdu_type) {
1229 case NS_PDUT_ALIVE:
Harald Welte2bffac52010-05-12 15:55:23 +00001230 /* If we're dead and blocked and suddenly receive a
1231 * NS-ALIVE out of the blue, we might have been re-started
1232 * and should send a NS-RESET to make sure everything recovers
1233 * fine. */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001234 if ((*nsvc)->state == NSE_S_BLOCKED)
1235 rc = gprs_ns_tx_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE);
Harald Welte2bffac52010-05-12 15:55:23 +00001236 else
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001237 rc = gprs_ns_tx_alive_ack(*nsvc);
Harald Welte9ba50052010-03-14 15:45:01 +08001238 break;
1239 case NS_PDUT_ALIVE_ACK:
Harald Welte90af1942010-05-15 23:02:24 +02001240 /* stop Tns-alive and start Tns-test */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001241 nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
1242 if ((*nsvc)->remote_end_is_sgsn) {
Harald Welte7fb7e612010-05-03 21:11:22 +02001243 /* FIXME: this should be one level higher */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001244 if ((*nsvc)->state & NSE_S_BLOCKED)
1245 rc = gprs_ns_tx_unblock(*nsvc);
Harald Welte7fb7e612010-05-03 21:11:22 +02001246 }
Harald Welte9ba50052010-03-14 15:45:01 +08001247 break;
1248 case NS_PDUT_UNITDATA:
1249 /* actual user data */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001250 rc = gprs_ns_rx_unitdata(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001251 break;
1252 case NS_PDUT_STATUS:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001253 rc = gprs_ns_rx_status(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001254 break;
1255 case NS_PDUT_RESET:
Harald Welte24a655f2010-04-30 19:54:29 +02001256 rc = gprs_ns_rx_reset(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001257 break;
1258 case NS_PDUT_RESET_ACK:
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001259 rc = gprs_ns_rx_reset_ack(nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001260 break;
1261 case NS_PDUT_UNBLOCK:
1262 /* Section 7.2: unblocking procedure */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001263 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei);
1264 (*nsvc)->state &= ~NSE_S_BLOCKED;
1265 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
1266 rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK);
Harald Welte9ba50052010-03-14 15:45:01 +08001267 break;
1268 case NS_PDUT_UNBLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001269 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei);
Harald Weltec1402a62010-05-12 11:48:44 +02001270 /* mark NS-VC as unblocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001271 (*nsvc)->state = NSE_S_ALIVE;
1272 (*nsvc)->remote_state = NSE_S_ALIVE;
1273 ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0);
Harald Welte9ba50052010-03-14 15:45:01 +08001274 break;
1275 case NS_PDUT_BLOCK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001276 rc = gprs_ns_rx_block(*nsvc, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001277 break;
1278 case NS_PDUT_BLOCK_ACK:
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001279 LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei);
Harald Weltef030b212010-04-26 19:18:54 +02001280 /* mark remote NS-VC as blocked + active */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001281 (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE;
Harald Welte9ba50052010-03-14 15:45:01 +08001282 break;
1283 default:
Harald Weltee4ecc8c2010-05-12 00:16:57 +02001284 LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n",
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +02001285 (*nsvc)->nsei, nsh->pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +08001286 rc = -EINVAL;
1287 break;
1288 }
1289 return rc;
1290}
1291
Harald Weltea9f23c82011-11-23 15:01:31 +01001292/*! \brief Create a new GPRS NS instance
1293 * \param[in] cb Call-back function for incoming BSSGP data
1294 * \returns dynamically allocated gprs_ns_inst
1295 */
Harald Welte4fcdd762012-06-16 16:40:42 +08001296struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx)
Harald Weltef030b212010-04-26 19:18:54 +02001297{
Harald Welte4fcdd762012-06-16 16:40:42 +08001298 struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst);
Harald Weltef030b212010-04-26 19:18:54 +02001299
1300 nsi->cb = cb;
1301 INIT_LLIST_HEAD(&nsi->gprs_nsvcs);
Harald Weltefe4ab902010-05-12 17:19:53 +00001302 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1303 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1304 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1305 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1306 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1307 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1308 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
Harald Weltef030b212010-04-26 19:18:54 +02001309
Harald Welte60cc6ac2010-05-13 14:20:56 +02001310 /* Create the dummy NSVC that we use for sending
1311 * messages to non-existant/unknown NS-VC's */
Harald Weltef5430362012-06-17 12:25:53 +08001312 nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe);
Harald Welte60cc6ac2010-05-13 14:20:56 +02001313 llist_del(&nsi->unknown_nsvc->list);
Harald Weltedd1c83c2010-05-13 13:58:08 +02001314
Harald Welte3771d092010-04-30 20:26:32 +02001315 return nsi;
Harald Weltef030b212010-04-26 19:18:54 +02001316}
1317
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001318void gprs_ns_close(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001319{
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001320 struct gprs_nsvc *nsvc, *nsvc2;
Harald Weltef030b212010-04-26 19:18:54 +02001321
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001322 /* delete all NSVCs and clear their timers */
1323 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list)
1324 gprs_nsvc_delete(nsvc);
1325
1326 /* close socket and unregister */
1327 if (nsi->nsip.fd.data) {
1328 close(nsi->nsip.fd.fd);
1329 osmo_fd_unregister(&nsi->nsip.fd);
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001330 nsi->nsip.fd.data = NULL;
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001331 }
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001332}
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001333
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +02001334/*! \brief Destroy an entire NS instance
1335 * \param nsi gprs_ns_inst that is to be destroyed
1336 *
1337 * This function releases all resources associated with the
1338 * NS-instance.
1339 */
1340void gprs_ns_destroy(struct gprs_ns_inst *nsi)
1341{
1342 gprs_ns_close(nsi);
Andreas Eversbergaaccdac2012-09-28 10:11:25 +02001343 /* free the NSI */
Harald Weltef030b212010-04-26 19:18:54 +02001344 talloc_free(nsi);
1345}
1346
1347
1348/* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2
1349 * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */
1350
1351/* Read a single NS-over-IP message */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001352static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error,
Harald Weltef030b212010-04-26 19:18:54 +02001353 struct sockaddr_in *saddr)
1354{
Harald Welteba4c6662010-05-19 15:38:10 +02001355 struct msgb *msg = gprs_ns_msgb_alloc();
Harald Weltef030b212010-04-26 19:18:54 +02001356 int ret = 0;
1357 socklen_t saddr_len = sizeof(*saddr);
1358
1359 if (!msg) {
1360 *error = -ENOMEM;
1361 return NULL;
1362 }
1363
Holger Hans Peter Freyther26c32512010-05-28 03:25:36 +08001364 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
Harald Weltef030b212010-04-26 19:18:54 +02001365 (struct sockaddr *)saddr, &saddr_len);
1366 if (ret < 0) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001367 LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n",
Harald Weltebbc9fac2010-05-03 18:54:12 +02001368 strerror(errno));
Harald Weltef030b212010-04-26 19:18:54 +02001369 msgb_free(msg);
1370 *error = ret;
1371 return NULL;
1372 } else if (ret == 0) {
1373 msgb_free(msg);
1374 *error = ret;
1375 return NULL;
1376 }
1377
1378 msg->l2h = msg->data;
1379 msgb_put(msg, ret);
1380
1381 return msg;
1382}
1383
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001384static int handle_nsip_read(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001385{
1386 int error;
1387 struct sockaddr_in saddr;
1388 struct gprs_ns_inst *nsi = bfd->data;
1389 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr);
1390
1391 if (!msg)
1392 return error;
1393
Harald Welteb3ee2652010-05-19 14:38:50 +02001394 error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP);
Harald Welte91813cf2010-05-12 18:38:45 +00001395
1396 msgb_free(msg);
1397
1398 return error;
Harald Weltef030b212010-04-26 19:18:54 +02001399}
1400
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001401static int handle_nsip_write(struct osmo_fd *bfd)
Harald Weltef030b212010-04-26 19:18:54 +02001402{
1403 /* FIXME: actually send the data here instead of nsip_sendmsg() */
1404 return -EIO;
1405}
1406
Harald Welteb3ee2652010-05-19 14:38:50 +02001407static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg)
Harald Weltef030b212010-04-26 19:18:54 +02001408{
1409 int rc;
1410 struct gprs_ns_inst *nsi = nsvc->nsi;
1411 struct sockaddr_in *daddr = &nsvc->ip.bts_addr;
1412
1413 rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0,
1414 (struct sockaddr *)daddr, sizeof(*daddr));
1415
Holger Hans Peter Freyther52746462012-03-01 20:30:32 +01001416 msgb_free(msg);
Harald Weltef030b212010-04-26 19:18:54 +02001417
1418 return rc;
1419}
1420
1421/* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +02001422static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Weltef030b212010-04-26 19:18:54 +02001423{
1424 int rc = 0;
1425
1426 if (what & BSC_FD_READ)
1427 rc = handle_nsip_read(bfd);
1428 if (what & BSC_FD_WRITE)
1429 rc = handle_nsip_write(bfd);
1430
1431 return rc;
1432}
1433
Harald Weltea9f23c82011-11-23 15:01:31 +01001434/*! \brief Create a listening socket for GPRS NS/UDP/IP
1435 * \param[in] nsi NS protocol instance to listen
1436 * \returns >=0 (fd) in case of success, negative in case of error
1437 *
1438 * A call to this function will create a UDP socket bound to the port
1439 * number and IP address specified in the NS protocol instance. The
1440 * file descriptor of the socket will be stored in nsi->nsip.fd.
1441 */
Harald Welte7fb05232010-05-19 15:09:09 +02001442int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi)
Harald Weltef030b212010-04-26 19:18:54 +02001443{
Harald Welte73952e32012-06-16 14:59:56 +08001444 struct in_addr in;
Harald Weltef030b212010-04-26 19:18:54 +02001445 int ret;
1446
Harald Welte73952e32012-06-16 14:59:56 +08001447 in.s_addr = htonl(nsi->nsip.local_ip);
1448
1449 nsi->nsip.fd.cb = nsip_fd_cb;
1450 nsi->nsip.fd.data = nsi;
1451 ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM,
1452 IPPROTO_UDP, inet_ntoa(in),
1453 nsi->nsip.local_port, OSMO_SOCK_F_BIND);
Harald Weltef030b212010-04-26 19:18:54 +02001454 if (ret < 0)
1455 return ret;
1456
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +01001457 ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS,
1458 &nsi->nsip.dscp, sizeof(nsi->nsip.dscp));
1459 if (ret < 0)
1460 LOGP(DNS, LOGL_ERROR,
1461 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
1462 nsi->nsip.dscp, ret, errno);
1463
Harald Weltef030b212010-04-26 19:18:54 +02001464
1465 return ret;
1466}
Harald Welte3771d092010-04-30 20:26:32 +02001467
Harald Weltea9f23c82011-11-23 15:01:31 +01001468/*! \brief Initiate a RESET procedure
1469 * \param[in] nsvc NS-VC in which to start the procedure
1470 * \param[in] cause Numeric NS cause value
1471 *
1472 * This is a high-level function initiating a NS-RESET procedure. It
1473 * will not only send a NS-RESET, but also set the state to BLOCKED and
1474 * start the Tns-reset timer.
1475 */
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001476void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause)
Harald Welte731d1fc2010-05-14 11:53:08 +00001477{
Harald Welteb1020d52010-05-25 22:17:30 +02001478 LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n",
1479 nsvc->nsei);
1480
Harald Welte731d1fc2010-05-14 11:53:08 +00001481 /* Mark NS-VC locally as blocked and dead */
1482 nsvc->state = NSE_S_BLOCKED;
1483 /* Send NS-RESET PDU */
1484 if (gprs_ns_tx_reset(nsvc, cause) < 0) {
1485 LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n",
1486 nsvc->nsei);
1487 }
1488 /* Start Tns-reset */
1489 nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET);
Harald Welte731d1fc2010-05-14 11:53:08 +00001490}
1491
Harald Weltea9f23c82011-11-23 15:01:31 +01001492/*! \brief Establish a NS connection (from the BSS) to the SGSN
1493 * \param nsi NS-instance
1494 * \param[in] dest Destination IP/Port
1495 * \param[in] nsei NSEI of the to-be-established NS-VC
1496 * \param[in] nsvci NSVCI of the to-be-established NS-VC
1497 * \returns struct gprs_nsvc representing the new NS-VC
1498 *
1499 * This function will establish a single NS/UDP/IP connection in uplink
1500 * (BSS to SGSN) direction.
1501 */
Harald Weltef5430362012-06-17 12:25:53 +08001502struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
Harald Welte1203de32010-05-01 11:28:43 +02001503 struct sockaddr_in *dest, uint16_t nsei,
1504 uint16_t nsvci)
Harald Welte3771d092010-04-30 20:26:32 +02001505{
1506 struct gprs_nsvc *nsvc;
1507
1508 nsvc = nsvc_by_rem_addr(nsi, dest);
Harald Welte38407ef2010-05-12 11:56:39 +00001509 if (!nsvc)
Harald Weltef5430362012-06-17 12:25:53 +08001510 nsvc = gprs_nsvc_create(nsi, nsvci);
Harald Welte38407ef2010-05-12 11:56:39 +00001511 nsvc->ip.bts_addr = *dest;
Harald Welte1203de32010-05-01 11:28:43 +02001512 nsvc->nsei = nsei;
1513 nsvc->nsvci = nsvci;
Jacob Erlbeck5405a102013-10-24 01:33:23 +02001514 nsvc->nsvci_is_valid = 1;
Harald Welte3771d092010-04-30 20:26:32 +02001515 nsvc->remote_end_is_sgsn = 1;
1516
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +08001517 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
1518 return nsvc;
Harald Welte3771d092010-04-30 20:26:32 +02001519}
Harald Weltea9f23c82011-11-23 15:01:31 +01001520
Harald Weltecca49632012-06-16 17:45:59 +08001521void gprs_ns_set_log_ss(int ss)
1522{
1523 DNS = ss;
1524}
1525
Harald Weltea9f23c82011-11-23 15:01:31 +01001526/*! }@ */