Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1 | /* GPRS Networks Service (NS) messages on the Gb interface |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */ |
| 3 | |
Harald Welte | 605ac5d | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 4 | /* (C) 2009-2012 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 9 | * 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 Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 11 | * (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 Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 16 | * GNU General Public License for more details. |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 17 | * |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License |
Harald Welte | e4cbb3f | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 23 | /*! \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 Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 34 | * 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 Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 56 | /* 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 Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 64 | #include <stdlib.h> |
| 65 | #include <unistd.h> |
| 66 | #include <errno.h> |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 67 | #include <stdint.h> |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 68 | |
Holger Hans Peter Freyther | 3c16de2 | 2012-07-12 14:13:41 +0200 | [diff] [blame] | 69 | #include <sys/types.h> |
| 70 | #include <sys/socket.h> |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 71 | #include <arpa/inet.h> |
| 72 | |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 73 | #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 Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 78 | #include <osmocom/core/socket.h> |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 79 | #include <osmocom/core/signal.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 80 | #include <osmocom/gprs/gprs_ns.h> |
| 81 | #include <osmocom/gprs/gprs_bssgp.h> |
| 82 | #include <osmocom/gprs/gprs_ns_frgre.h> |
| 83 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 84 | #include "common_vty.h" |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 85 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 86 | static 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 Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 96 | enum 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 Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 103 | NS_CTR_REPLACED, |
| 104 | NS_CTR_NSEI_CHG, |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 105 | NS_CTR_INV_VCI, |
| 106 | NS_CTR_INV_NSEI, |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 109 | static const struct rate_ctr_desc nsvc_ctr_description[] = { |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 110 | { "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 Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 117 | { "nsei-chg", "NS-VC changed NSEI count " }, |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 118 | { "inv-nsvci", "NS-VCI was invalid count " }, |
| 119 | { "inv-nsei", "NSEI was invalid count " }, |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static const struct rate_ctr_group_desc nsvc_ctrg_desc = { |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 123 | .group_name_prefix = "ns.nsvc", |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 124 | .group_description = "NSVC Peer Statistics", |
| 125 | .num_ctr = ARRAY_SIZE(nsvc_ctr_description), |
| 126 | .ctr_desc = nsvc_ctr_description, |
| 127 | }; |
| 128 | |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 129 | #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 | |
Jacob Erlbeck | 8d192d7 | 2015-04-07 17:52:45 +0200 | [diff] [blame] | 134 | struct msgb *gprs_ns_msgb_alloc(void) |
| 135 | { |
| 136 | struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM, |
| 137 | "GPRS/NS"); |
| 138 | if (!msg) { |
| 139 | LOGP(DNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n", |
| 140 | NS_ALLOC_SIZE); |
| 141 | } |
| 142 | return msg; |
| 143 | } |
| 144 | |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 145 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 146 | /*! \brief Lookup struct gprs_nsvc based on NSVCI |
| 147 | * \param[in] nsi NS instance in which to search |
| 148 | * \param[in] nsvci NSVCI to be searched |
| 149 | * \returns gprs_nsvc of respective NSVCI |
| 150 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 151 | struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 152 | { |
| 153 | struct gprs_nsvc *nsvc; |
| 154 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 155 | if (nsvc->nsvci == nsvci) |
| 156 | return nsvc; |
| 157 | } |
| 158 | return NULL; |
| 159 | } |
| 160 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 161 | /*! \brief Lookup struct gprs_nsvc based on NSEI |
| 162 | * \param[in] nsi NS instance in which to search |
| 163 | * \param[in] nsei NSEI to be searched |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 164 | * \returns first gprs_nsvc of respective NSEI |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 165 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 166 | struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei) |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 167 | { |
| 168 | struct gprs_nsvc *nsvc; |
| 169 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 170 | if (nsvc->nsei == nsei) |
| 171 | return nsvc; |
| 172 | } |
| 173 | return NULL; |
| 174 | } |
| 175 | |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 176 | static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi, |
| 177 | uint16_t nsei) |
| 178 | { |
| 179 | struct gprs_nsvc *nsvc; |
| 180 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 181 | if (nsvc->nsei == nsei) { |
Jacob Erlbeck | bf02196 | 2013-10-17 13:58:35 +0200 | [diff] [blame] | 182 | if (!(nsvc->state & NSE_S_BLOCKED) && |
| 183 | nsvc->state & NSE_S_ALIVE) |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 184 | return nsvc; |
| 185 | } |
| 186 | } |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 190 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 191 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 192 | struct sockaddr_in *sin) |
| 193 | { |
| 194 | struct gprs_nsvc *nsvc; |
| 195 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 196 | if (nsvc->ip.bts_addr.sin_addr.s_addr == |
| 197 | sin->sin_addr.s_addr && |
| 198 | nsvc->ip.bts_addr.sin_port == sin->sin_port) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 199 | return nsvc; |
| 200 | } |
| 201 | return NULL; |
| 202 | } |
| 203 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 204 | static void gprs_ns_timer_cb(void *data); |
| 205 | |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 206 | struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 207 | { |
| 208 | struct gprs_nsvc *nsvc; |
| 209 | |
Harald Welte | b1020d5 | 2010-05-25 22:17:30 +0200 | [diff] [blame] | 210 | LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci); |
| 211 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 212 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 213 | nsvc->nsvci = nsvci; |
Jacob Erlbeck | 9b591b7 | 2013-11-11 09:43:05 +0100 | [diff] [blame] | 214 | nsvc->nsvci_is_valid = 1; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 215 | /* before RESET procedure: BLOCKED and DEAD */ |
| 216 | nsvc->state = NSE_S_BLOCKED; |
| 217 | nsvc->nsi = nsi; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 218 | nsvc->timer.cb = gprs_ns_timer_cb; |
| 219 | nsvc->timer.data = nsvc; |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 220 | nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 221 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 222 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 223 | |
| 224 | return nsvc; |
| 225 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 226 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 227 | /*! \brief Delete given NS-VC |
| 228 | * \param[in] nsvc gprs_nsvc to be deleted |
| 229 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 230 | void gprs_nsvc_delete(struct gprs_nsvc *nsvc) |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 231 | { |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 232 | if (osmo_timer_pending(&nsvc->timer)) |
| 233 | osmo_timer_del(&nsvc->timer); |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 234 | llist_del(&nsvc->list); |
Jacob Erlbeck | a52ba01 | 2013-10-24 01:33:21 +0200 | [diff] [blame] | 235 | rate_ctr_group_free(nsvc->ctrg); |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 236 | talloc_free(nsvc); |
| 237 | } |
| 238 | |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 239 | static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal, |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 240 | uint8_t cause) |
| 241 | { |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 242 | struct ns_signal_data nssd = {0}; |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 243 | |
| 244 | nssd.nsvc = nsvc; |
| 245 | nssd.cause = cause; |
| 246 | |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 247 | osmo_signal_dispatch(SS_L_NS, signal, &nssd); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 250 | static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc, |
| 251 | struct msgb *msg, |
| 252 | uint8_t pdu_type, uint8_t ie_type) |
| 253 | { |
| 254 | struct ns_signal_data nssd = {0}; |
| 255 | |
| 256 | nssd.nsvc = nsvc; |
| 257 | nssd.pdu_type = pdu_type; |
| 258 | nssd.ie_type = ie_type; |
| 259 | nssd.msg = msg; |
| 260 | |
| 261 | osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd); |
| 262 | } |
| 263 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 264 | static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc) |
| 265 | { |
| 266 | struct ns_signal_data nssd = {0}; |
| 267 | |
| 268 | nssd.nsvc = nsvc; |
| 269 | nssd.old_nsvc = old_nsvc; |
| 270 | |
| 271 | osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd); |
| 272 | } |
| 273 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 274 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 275 | static const struct value_string ns_cause_str[] = { |
| 276 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 277 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 278 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 279 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 280 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 281 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 282 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 283 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 284 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 285 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 286 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 287 | { 0, NULL } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 288 | }; |
| 289 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 290 | /*! \brief Obtain a human-readable string for NS cause value */ |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 291 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 292 | { |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 293 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 294 | } |
| 295 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 296 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 297 | extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 298 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 299 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 300 | { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 301 | int ret; |
| 302 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 303 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 304 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 305 | /* Increment number of Uplink bytes */ |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 306 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]); |
| 307 | rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg)); |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 308 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 309 | switch (nsvc->ll) { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 310 | case GPRS_NS_LL_UDP: |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 311 | ret = nsip_sendmsg(nsvc, msg); |
Jacob Erlbeck | 8d192d7 | 2015-04-07 17:52:45 +0200 | [diff] [blame] | 312 | if (ret < 0) |
| 313 | LOGP(DNS, LOGL_INFO, |
| 314 | "failed to send NS message via UDP: %s\n", |
| 315 | strerror(-ret)); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 316 | break; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 317 | case GPRS_NS_LL_FR_GRE: |
| 318 | ret = gprs_ns_frgre_sendmsg(nsvc, msg); |
Jacob Erlbeck | 8d192d7 | 2015-04-07 17:52:45 +0200 | [diff] [blame] | 319 | if (ret < 0) |
| 320 | LOGP(DNS, LOGL_INFO, |
| 321 | "failed to send NS message via FR/GRE: %s\n", |
| 322 | strerror(-ret)); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 323 | break; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 324 | default: |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 325 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 326 | msgb_free(msg); |
| 327 | ret = -EIO; |
| 328 | break; |
| 329 | } |
| 330 | return ret; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 331 | } |
| 332 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 333 | static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 334 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 335 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 336 | struct gprs_ns_hdr *nsh; |
| 337 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 338 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 339 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 340 | if (!msg) |
| 341 | return -ENOMEM; |
| 342 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 343 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 344 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 345 | |
| 346 | nsh->pdu_type = pdu_type; |
| 347 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 348 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 349 | } |
| 350 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 351 | /*! \brief Transmit a NS-RESET on a given NSVC |
| 352 | * \param[in] nsvc NS-VC used for transmission |
| 353 | * \paam[in] cause Numeric NS cause value |
| 354 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 355 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 356 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 357 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 358 | struct gprs_ns_hdr *nsh; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 359 | uint16_t nsvci = htons(nsvc->nsvci); |
| 360 | uint16_t nsei = htons(nsvc->nsei); |
| 361 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 362 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 363 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 364 | if (!msg) |
| 365 | return -ENOMEM; |
| 366 | |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 367 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n", |
| 368 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 369 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 370 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 371 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 372 | nsh->pdu_type = NS_PDUT_RESET; |
| 373 | |
| 374 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 375 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 376 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 377 | |
| 378 | return gprs_ns_tx(nsvc, msg); |
| 379 | |
| 380 | } |
| 381 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 382 | /*! \brief Transmit a NS-STATUS on a given NSVC |
| 383 | * \param[in] nsvc NS-VC to be used for transmission |
| 384 | * \param[in] cause Numeric NS cause value |
| 385 | * \param[in] bvci BVCI to be reset within NSVC |
| 386 | * \param[in] orig_msg message causing the STATUS */ |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 387 | int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause, |
| 388 | uint16_t bvci, struct msgb *orig_msg) |
| 389 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 390 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 391 | struct gprs_ns_hdr *nsh; |
| 392 | uint16_t nsvci = htons(nsvc->nsvci); |
| 393 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 394 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 395 | |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 396 | bvci = htons(bvci); |
| 397 | |
| 398 | if (!msg) |
| 399 | return -ENOMEM; |
| 400 | |
Harald Welte | 90af194 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 401 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n", |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 402 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 403 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 404 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 405 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 406 | nsh->pdu_type = NS_PDUT_STATUS; |
| 407 | |
| 408 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 409 | |
| 410 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 411 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 412 | cause == NS_CAUSE_NSVC_UNKNOWN) |
| 413 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 414 | |
| 415 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 416 | switch (cause) { |
| 417 | case NS_CAUSE_SEM_INCORR_PDU: |
| 418 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 419 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 420 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 421 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 422 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 423 | orig_msg->l2h); |
| 424 | break; |
| 425 | default: |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | /* Section 9.2.7.3: Static conditions for BVCI */ |
| 430 | if (cause == NS_CAUSE_BVCI_UNKNOWN) |
| 431 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci); |
| 432 | |
| 433 | return gprs_ns_tx(nsvc, msg); |
| 434 | } |
| 435 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 436 | /*! \brief Transmit a NS-BLOCK on a tiven NS-VC |
| 437 | * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted |
| 438 | * \param[in] cause Numeric NS Cause value |
| 439 | * \returns 0 in case of success |
| 440 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 441 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause) |
| 442 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 443 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 444 | struct gprs_ns_hdr *nsh; |
| 445 | uint16_t nsvci = htons(nsvc->nsvci); |
| 446 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 447 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 448 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 449 | if (!msg) |
| 450 | return -ENOMEM; |
| 451 | |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 452 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n", |
| 453 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 454 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 455 | /* be conservative and mark it as blocked even now! */ |
| 456 | nsvc->state |= NSE_S_BLOCKED; |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 457 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 458 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 459 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 460 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 461 | nsh->pdu_type = NS_PDUT_BLOCK; |
| 462 | |
| 463 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 464 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 465 | |
| 466 | return gprs_ns_tx(nsvc, msg); |
| 467 | } |
| 468 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 469 | /*! \brief Transmit a NS-UNBLOCK on a given NS-VC |
| 470 | * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted |
| 471 | * \returns 0 in case of success |
| 472 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 473 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc) |
| 474 | { |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 475 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 476 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n", |
| 477 | nsvc->nsei, nsvc->nsvci); |
| 478 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 479 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 480 | } |
| 481 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 482 | /*! \brief Transmit a NS-ALIVE on a given NS-VC |
| 483 | * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted |
| 484 | * \returns 0 in case of success |
| 485 | */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 486 | int gprs_ns_tx_alive(struct gprs_nsvc *nsvc) |
| 487 | { |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 488 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 489 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n", |
| 490 | nsvc->nsei, nsvc->nsvci); |
| 491 | |
| 492 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 493 | } |
| 494 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 495 | /*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC |
| 496 | * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted |
| 497 | * \returns 0 in case of success |
| 498 | */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 499 | int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc) |
| 500 | { |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 501 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 502 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n", |
| 503 | nsvc->nsei, nsvc->nsvci); |
| 504 | |
| 505 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
| 506 | } |
| 507 | |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 508 | static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = { |
| 509 | [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET, |
| 510 | [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE, |
| 511 | [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST, |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 512 | }; |
| 513 | |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 514 | static const struct value_string timer_mode_strs[] = { |
| 515 | { NSVC_TIMER_TNS_RESET, "tns-reset" }, |
| 516 | { NSVC_TIMER_TNS_ALIVE, "tns-alive" }, |
| 517 | { NSVC_TIMER_TNS_TEST, "tns-test" }, |
| 518 | { 0, NULL } |
| 519 | }; |
| 520 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 521 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 522 | { |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 523 | enum ns_timeout tout = timer_mode_tout[mode]; |
| 524 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
| 525 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 526 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 4e187c6 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 527 | DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n", |
| 528 | nsvc->nsei, get_value_string(timer_mode_strs, mode), |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 529 | seconds); |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 530 | |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 531 | if (osmo_timer_pending(&nsvc->timer)) |
| 532 | osmo_timer_del(&nsvc->timer); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 533 | |
| 534 | nsvc->timer_mode = mode; |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 535 | osmo_timer_schedule(&nsvc->timer, seconds, 0); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 536 | } |
| 537 | |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 538 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 539 | { |
| 540 | struct gprs_nsvc *nsvc = data; |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 541 | enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode]; |
| 542 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 543 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 544 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 4e187c6 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 545 | DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n", |
| 546 | nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode), |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 547 | seconds); |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 548 | |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 549 | switch (nsvc->timer_mode) { |
| 550 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 551 | /* Tns-alive case: we expired without response ! */ |
| 552 | nsvc->alive_retries++; |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 553 | if (nsvc->alive_retries > |
| 554 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 555 | /* mark as dead and blocked */ |
| 556 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 557 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
| 558 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]); |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 559 | LOGP(DNS, LOGL_NOTICE, |
| 560 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 561 | "%u times, blocking NS-VC\n", nsvc->nsei, |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 562 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]); |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 563 | ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0); |
| 564 | ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 565 | return; |
| 566 | } |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 567 | /* Tns-test case: send NS-ALIVE PDU */ |
| 568 | gprs_ns_tx_alive(nsvc); |
| 569 | /* start Tns-alive timer */ |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 570 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 571 | break; |
| 572 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 573 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 574 | gprs_ns_tx_alive(nsvc); |
| 575 | /* start Tns-alive timer (transition into faster |
| 576 | * alive retransmissions) */ |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 577 | nsvc->alive_retries = 0; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 578 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 579 | break; |
| 580 | case NSVC_TIMER_TNS_RESET: |
| 581 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 570fb83 | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 582 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 583 | /* Re-start Tns-reset timer */ |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 584 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 585 | break; |
Harald Welte | 2fc725d | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 586 | case _NSVC_TIMER_NR: |
| 587 | break; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 588 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* Section 9.2.6 */ |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 592 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 593 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 594 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 595 | struct gprs_ns_hdr *nsh; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 596 | uint16_t nsvci, nsei; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 597 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 598 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 599 | if (!msg) |
| 600 | return -ENOMEM; |
| 601 | |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 602 | nsvci = htons(nsvc->nsvci); |
| 603 | nsei = htons(nsvc->nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 604 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 605 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 606 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 607 | |
| 608 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 609 | |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 610 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n", |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 611 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 612 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 613 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 614 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 615 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 616 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 617 | } |
| 618 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 619 | /*! \brief High-level function for transmitting a NS-UNITDATA messsage |
| 620 | * \param[in] nsi NS-instance on which we shall transmit |
| 621 | * \param[in] msg struct msgb to be trasnmitted |
| 622 | * |
| 623 | * This function obtains the NS-VC by the msgb_nsei(msg) and then checks |
| 624 | * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS |
| 625 | * header for the NS-UNITDATA message type and sends it off. |
| 626 | * |
| 627 | * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive |
| 628 | */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 629 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 630 | { |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 631 | struct gprs_nsvc *nsvc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 632 | struct gprs_ns_hdr *nsh; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 633 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 634 | |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 635 | nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg)); |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 636 | if (!nsvc) { |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 637 | int rc; |
| 638 | if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) { |
| 639 | LOGP(DNS, LOGL_ERROR, |
| 640 | "All NS-VCs for NSEI %u are either dead or blocked!\n", |
| 641 | msgb_nsei(msg)); |
| 642 | rc = -EBUSY; |
| 643 | } else { |
| 644 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
| 645 | "to NS-VC!\n", msgb_nsei(msg)); |
| 646 | rc = -EINVAL; |
| 647 | } |
| 648 | |
Harald Welte | f47df64 | 2010-08-09 21:15:40 +0800 | [diff] [blame] | 649 | msgb_free(msg); |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 650 | return rc; |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 651 | } |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 652 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 653 | |
Harald Welte | ec20ba4 | 2010-05-13 12:18:49 +0200 | [diff] [blame] | 654 | msg->l2h = msgb_push(msg, sizeof(*nsh) + 3); |
| 655 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 656 | if (!nsh) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 657 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | f47df64 | 2010-08-09 21:15:40 +0800 | [diff] [blame] | 658 | msgb_free(msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 659 | return -EIO; |
| 660 | } |
| 661 | |
| 662 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 663 | /* spare octet in data[0] */ |
| 664 | nsh->data[1] = bvci >> 8; |
| 665 | nsh->data[2] = bvci & 0xff; |
| 666 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 667 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | /* Section 9.2.10: receive side */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 671 | static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 672 | { |
| 673 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 674 | uint16_t bvci; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 675 | |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 676 | if (nsvc->state & NSE_S_BLOCKED) |
| 677 | return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED, |
| 678 | 0, msg); |
| 679 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 680 | /* spare octet in data[0] */ |
| 681 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | ec19c10 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 682 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 683 | msgb_bvci(msg) = bvci; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 684 | |
| 685 | /* call upper layer (BSSGP) */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 686 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* Section 9.2.7 */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 690 | static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 691 | { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 692 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 693 | struct tlv_parsed tp; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 694 | uint8_t cause; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 695 | int rc; |
| 696 | |
Harald Welte | 4133783 | 2010-05-16 00:24:26 +0200 | [diff] [blame] | 697 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 698 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 699 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 700 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 701 | if (rc < 0) { |
| 702 | LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n"); |
| 703 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: " |
| 704 | "Error during TLV Parse\n", nsvc->nsei); |
| 705 | return rc; |
| 706 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 707 | |
| 708 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 709 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 710 | return -EINVAL; |
| 711 | } |
| 712 | |
| 713 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | 4133783 | 2010-05-16 00:24:26 +0200 | [diff] [blame] | 714 | LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 719 | /* Replace a nsvc object with another based on NSVCI. |
| 720 | * This function replaces looks for a NSVC with the given NSVCI and replaces it |
| 721 | * if possible and necessary. If replaced, the former value of *nsvc is |
| 722 | * returned in *old_nsvc. |
| 723 | * \return != 0 if *nsvc points to a matching NSVC. |
| 724 | */ |
| 725 | static int gprs_nsvc_replace_if_found(uint16_t nsvci, |
| 726 | struct gprs_nsvc **nsvc, |
| 727 | struct gprs_nsvc **old_nsvc) |
| 728 | { |
| 729 | struct gprs_nsvc *matching_nsvc; |
| 730 | |
| 731 | if ((*nsvc)->nsvci == nsvci) { |
| 732 | *old_nsvc = NULL; |
| 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci); |
| 737 | |
| 738 | if (!matching_nsvc) |
| 739 | return 0; |
| 740 | |
| 741 | /* The NS-VCI is already used by this NS-VC */ |
| 742 | |
| 743 | char *old_peer; |
| 744 | |
| 745 | /* Exchange the NS-VC objects */ |
| 746 | *old_nsvc = *nsvc; |
| 747 | *nsvc = matching_nsvc; |
| 748 | |
| 749 | /* Do logging */ |
| 750 | old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc)); |
| 751 | LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n", |
| 752 | nsvci, old_peer, gprs_ns_ll_str(*nsvc)); |
| 753 | |
| 754 | talloc_free(old_peer); |
| 755 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 756 | return 1; |
| 757 | } |
| 758 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 759 | /* Section 7.3 */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 760 | static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 761 | { |
| 762 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 763 | struct tlv_parsed tp; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 764 | uint8_t cause; |
| 765 | uint16_t nsvci, nsei; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 766 | struct gprs_nsvc *orig_nsvc = NULL; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 767 | int rc; |
| 768 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 769 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 770 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 771 | if (rc < 0) { |
| 772 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET " |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 773 | "Error during TLV Parse\n", (*nsvc)->nsei); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 774 | return rc; |
| 775 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 776 | |
| 777 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 778 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 779 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 780 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 781 | gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 782 | return -EINVAL; |
| 783 | } |
| 784 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 785 | cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 786 | nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI)); |
| 787 | nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI)); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 788 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 789 | LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n", |
| 790 | (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)", |
| 791 | nsei, nsvci, gprs_ns_cause_str(cause)); |
| 792 | |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 793 | if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) { |
| 794 | if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) { |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 795 | /* The incoming RESET doesn't match the NSVCI. Send an |
| 796 | * appropriate RESET_ACK and ignore the RESET. |
| 797 | * See 3GPP TS 08.16, 7.3.1, 2nd paragraph. |
| 798 | */ |
| 799 | ns_osmo_signal_dispatch_mismatch(*nsvc, msg, |
| 800 | NS_PDUT_RESET, |
| 801 | NS_IE_VCI); |
| 802 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]); |
| 803 | gprs_ns_tx_reset_ack(*nsvc); |
| 804 | return 0; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 805 | } |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 806 | |
| 807 | /* NS-VCI has changed */ |
Jacob Erlbeck | 54b8b2d | 2013-10-24 01:33:25 +0200 | [diff] [blame] | 808 | if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) { |
| 809 | LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d " |
| 810 | "at %s\n", |
| 811 | nsvci, (*nsvc)->nsvci, |
| 812 | gprs_ns_ll_str(*nsvc)); |
| 813 | orig_nsvc = *nsvc; |
| 814 | *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci); |
Jacob Erlbeck | 54b8b2d | 2013-10-24 01:33:25 +0200 | [diff] [blame] | 815 | (*nsvc)->nsei = nsei; |
| 816 | } |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 817 | } |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 818 | |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 819 | if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) { |
| 820 | if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) { |
| 821 | /* The incoming RESET doesn't match the NSEI. Send an |
| 822 | * appropriate RESET_ACK and ignore the RESET. |
| 823 | * See 3GPP TS 08.16, 7.3.1, 3rd paragraph. |
| 824 | */ |
| 825 | ns_osmo_signal_dispatch_mismatch(*nsvc, msg, |
| 826 | NS_PDUT_RESET, |
| 827 | NS_IE_NSEI); |
| 828 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]); |
Holger Hans Peter Freyther | 726e272 | 2013-10-25 11:05:10 +0200 | [diff] [blame] | 829 | rc = gprs_ns_tx_reset_ack(*nsvc); |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 830 | CHECK_TX_RC(rc, *nsvc); |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | /* NSEI has changed */ |
| 835 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]); |
| 836 | (*nsvc)->nsei = nsei; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 837 | } |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 838 | |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 839 | /* Mark NS-VC as blocked and alive */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 840 | (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | e8b9ca2 | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 841 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 842 | if (orig_nsvc) { |
Jacob Erlbeck | 54b8b2d | 2013-10-24 01:33:25 +0200 | [diff] [blame] | 843 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 844 | ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 845 | |
| 846 | /* Update the ll info fields */ |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 847 | gprs_ns_ll_copy(*nsvc, orig_nsvc); |
| 848 | gprs_ns_ll_clear(orig_nsvc); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 849 | } else { |
| 850 | (*nsvc)->nsei = nsei; |
| 851 | (*nsvc)->nsvci = nsvci; |
| 852 | (*nsvc)->nsvci_is_valid = 1; |
| 853 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 854 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 855 | /* inform interested parties about the fact that this NSVC |
| 856 | * has received RESET */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 857 | ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 858 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 859 | rc = gprs_ns_tx_reset_ack(*nsvc); |
Jacob Erlbeck | 05395a6 | 2013-10-08 12:04:45 +0200 | [diff] [blame] | 860 | |
| 861 | /* start the test procedure */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 862 | gprs_ns_tx_simple((*nsvc), NS_PDUT_ALIVE); |
| 863 | nsvc_start_timer((*nsvc), NSVC_TIMER_TNS_TEST); |
Jacob Erlbeck | 05395a6 | 2013-10-08 12:04:45 +0200 | [diff] [blame] | 864 | |
| 865 | return rc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 866 | } |
| 867 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 868 | static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg) |
| 869 | { |
| 870 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 871 | struct tlv_parsed tp; |
| 872 | uint16_t nsvci, nsei; |
| 873 | struct gprs_nsvc *orig_nsvc = NULL; |
| 874 | int rc; |
| 875 | |
| 876 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 877 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 878 | if (rc < 0) { |
| 879 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK " |
| 880 | "Error during TLV Parse\n", (*nsvc)->nsei); |
| 881 | return rc; |
| 882 | } |
| 883 | |
| 884 | if (!TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 885 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
| 886 | LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n"); |
Holger Hans Peter Freyther | 7c91bfd | 2013-10-25 11:02:05 +0200 | [diff] [blame] | 887 | rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 888 | CHECK_TX_RC(rc, *nsvc); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 889 | return -EINVAL; |
| 890 | } |
| 891 | |
| 892 | nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI)); |
| 893 | nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI)); |
| 894 | |
| 895 | LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n", |
| 896 | (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)", |
| 897 | nsei, nsvci); |
| 898 | |
| 899 | if (!((*nsvc)->state & NSE_S_RESET)) { |
| 900 | /* Not waiting for a RESET_ACK on this NS-VC, ignore it. |
| 901 | * See 3GPP TS 08.16, 7.3.1, 5th paragraph. |
| 902 | */ |
| 903 | LOGP(DNS, LOGL_ERROR, |
| 904 | "NS RESET ACK Discarding unexpected message for " |
| 905 | "NS-VCI %d from SGSN NSEI=%d\n", |
| 906 | nsvci, nsei); |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | if (!(*nsvc)->nsvci_is_valid) { |
| 911 | LOGP(DNS, LOGL_NOTICE, |
| 912 | "NS RESET ACK Uninitialised NS-VC (%u) for " |
| 913 | "NS-VCI %d, NSEI=%d from %s\n", |
| 914 | (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc)); |
| 915 | return -EINVAL; |
| 916 | } |
| 917 | |
| 918 | if ((*nsvc)->nsvci != nsvci) { |
| 919 | /* NS-VCI has changed */ |
| 920 | |
| 921 | /* if !0, use another NSVC object that matches the NSVCI */ |
| 922 | int use_other_nsvc; |
| 923 | |
| 924 | /* Only do this with BSS peers */ |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 925 | use_other_nsvc = !(*nsvc)->remote_end_is_sgsn && |
| 926 | !(*nsvc)->persistent; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 927 | |
| 928 | if (use_other_nsvc) |
| 929 | /* Update *nsvc to point to the right NSVC object */ |
| 930 | use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc, |
| 931 | &orig_nsvc); |
| 932 | |
| 933 | if (!use_other_nsvc) { |
| 934 | /* The incoming RESET_ACK doesn't match the NSVCI. |
| 935 | * See 3GPP TS 08.16, 7.3.1, 4th paragraph. |
| 936 | */ |
| 937 | ns_osmo_signal_dispatch_mismatch(*nsvc, msg, |
| 938 | NS_PDUT_RESET_ACK, |
| 939 | NS_IE_VCI); |
| 940 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]); |
| 941 | LOGP(DNS, LOGL_ERROR, |
| 942 | "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) " |
| 943 | "from %s\n", |
| 944 | nsvci, |
| 945 | (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS", |
| 946 | nsei, gprs_ns_ll_str(*nsvc)); |
| 947 | return -EINVAL; |
| 948 | } |
| 949 | |
| 950 | /* Notify others */ |
Jacob Erlbeck | 54b8b2d | 2013-10-24 01:33:25 +0200 | [diff] [blame] | 951 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 952 | ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc); |
| 953 | |
| 954 | /* Update the ll info fields */ |
| 955 | gprs_ns_ll_copy(*nsvc, orig_nsvc); |
| 956 | gprs_ns_ll_clear(orig_nsvc); |
| 957 | } else if ((*nsvc)->nsei != nsei) { |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 958 | if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) { |
| 959 | /* The incoming RESET_ACK doesn't match the NSEI. |
| 960 | * See 3GPP TS 08.16, 7.3.1, 4th paragraph. |
| 961 | */ |
| 962 | ns_osmo_signal_dispatch_mismatch(*nsvc, msg, |
| 963 | NS_PDUT_RESET_ACK, |
| 964 | NS_IE_NSEI); |
| 965 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]); |
| 966 | LOGP(DNS, LOGL_ERROR, |
| 967 | "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n", |
| 968 | nsei, nsvci, gprs_ns_ll_str(*nsvc)); |
| 969 | return -EINVAL; |
| 970 | } |
| 971 | |
| 972 | /* NSEI has changed */ |
| 973 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]); |
| 974 | (*nsvc)->nsei = nsei; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* Mark NS-VC as blocked and alive */ |
| 978 | (*nsvc)->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
| 979 | (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
| 980 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]); |
| 981 | if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) { |
| 982 | /* stop RESET timer */ |
| 983 | osmo_timer_del(&(*nsvc)->timer); |
| 984 | } |
| 985 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
| 986 | rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_ALIVE); |
| 987 | nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST); |
| 988 | |
| 989 | return rc; |
| 990 | } |
| 991 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 992 | static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 993 | { |
| 994 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 995 | struct tlv_parsed tp; |
| 996 | uint8_t *cause; |
| 997 | int rc; |
| 998 | |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 999 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1000 | |
| 1001 | nsvc->state |= NSE_S_BLOCKED; |
| 1002 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 1003 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 1004 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 1005 | if (rc < 0) { |
| 1006 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK " |
| 1007 | "Error during TLV Parse\n", nsvc->nsei); |
| 1008 | return rc; |
| 1009 | } |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1010 | |
| 1011 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 1012 | !TLVP_PRESENT(&tp, NS_IE_VCI)) { |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1013 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 1014 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1015 | return -EINVAL; |
| 1016 | } |
| 1017 | |
| 1018 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 1019 | //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 1020 | |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 1021 | ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1022 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1023 | |
| 1024 | return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK); |
| 1025 | } |
| 1026 | |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1027 | int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 1028 | struct gprs_nsvc *fallback_nsvc, |
| 1029 | struct gprs_nsvc **new_nsvc); |
| 1030 | |
| 1031 | int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg, |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1032 | struct gprs_nsvc **nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1033 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1034 | /*! \brief Receive incoming NS message from underlying transport layer |
| 1035 | * \param nsi NS instance to which the data belongs |
| 1036 | * \param[in] msg message buffer containing newly-received data |
| 1037 | * \param[in] saddr socketaddr from which data was received |
| 1038 | * \param[in] ll link-layer type in which data was received |
| 1039 | * \returns 0 in case of success, < 0 in case of error |
| 1040 | * |
| 1041 | * This is the main entry point int othe NS imlementation where frames |
| 1042 | * from the underlying transport (normally UDP) enter. |
| 1043 | */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1044 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 1045 | struct sockaddr_in *saddr, enum gprs_ns_ll ll) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1046 | { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1047 | struct gprs_nsvc *nsvc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1048 | int rc = 0; |
| 1049 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1050 | /* look up the NSVC based on source address */ |
| 1051 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1052 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1053 | if (!nsvc) { |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1054 | struct gprs_nsvc *fallback_nsvc; |
| 1055 | |
| 1056 | fallback_nsvc = nsi->unknown_nsvc; |
| 1057 | log_set_context(GPRS_CTX_NSVC, fallback_nsvc); |
| 1058 | fallback_nsvc->ip.bts_addr = *saddr; |
| 1059 | fallback_nsvc->ll = ll; |
| 1060 | |
| 1061 | rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc); |
| 1062 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1063 | if (rc < 0) |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 1064 | return rc; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1065 | |
| 1066 | rc = 0; |
| 1067 | } |
| 1068 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1069 | if (nsvc) |
| 1070 | rc = gprs_ns_process_msg(nsi, msg, &nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1071 | |
| 1072 | return rc; |
| 1073 | } |
| 1074 | |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1075 | const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc) |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1076 | { |
| 1077 | static char buf[80]; |
| 1078 | snprintf(buf, sizeof(buf), "%s:%u", |
| 1079 | inet_ntoa(nsvc->ip.bts_addr.sin_addr), |
| 1080 | ntohs(nsvc->ip.bts_addr.sin_port)); |
Holger Hans Peter Freyther | 0cccf40 | 2013-10-25 11:00:23 +0200 | [diff] [blame] | 1081 | buf[sizeof(buf) - 1] = '\0'; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1082 | |
| 1083 | return buf; |
| 1084 | } |
| 1085 | |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1086 | void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other) |
| 1087 | { |
| 1088 | nsvc->ll = other->ll; |
| 1089 | |
| 1090 | switch (nsvc->ll) { |
| 1091 | case GPRS_NS_LL_UDP: |
| 1092 | nsvc->ip = other->ip; |
| 1093 | break; |
| 1094 | case GPRS_NS_LL_FR_GRE: |
| 1095 | nsvc->frgre = other->frgre; |
| 1096 | break; |
| 1097 | default: |
| 1098 | break; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | void gprs_ns_ll_clear(struct gprs_nsvc *nsvc) |
| 1103 | { |
| 1104 | switch (nsvc->ll) { |
| 1105 | case GPRS_NS_LL_UDP: |
| 1106 | nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY; |
| 1107 | nsvc->ip.bts_addr.sin_port = 0; |
| 1108 | break; |
| 1109 | case GPRS_NS_LL_FR_GRE: |
| 1110 | nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY; |
| 1111 | nsvc->frgre.bts_addr.sin_port = 0; |
| 1112 | break; |
| 1113 | default: |
| 1114 | break; |
| 1115 | } |
| 1116 | } |
| 1117 | |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1118 | /*! \brief Create/get NS-VC independently from underlying transport layer |
| 1119 | * \param nsi NS instance to which the data belongs |
| 1120 | * \param[in] msg message buffer containing newly-received data |
| 1121 | * \param[in] fallback_nsvc is used to send error messages back to the peer |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1122 | * and to initialise the ll info of a created NS-VC object |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1123 | * \param[out] new_nsvc contains a pointer to a NS-VC object if one has |
| 1124 | * been created or found |
| 1125 | * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been |
| 1126 | * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and |
| 1127 | * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object |
| 1128 | * has been created and registered, and GPRS_NS_CS_FOUND if an |
| 1129 | * existing NS-VC object has been found with the same NSEI. |
| 1130 | * |
| 1131 | * This contains the initial NS automaton state (NS-VC not yet attached). |
| 1132 | */ |
| 1133 | int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 1134 | struct gprs_nsvc *fallback_nsvc, |
| 1135 | struct gprs_nsvc **new_nsvc) |
| 1136 | { |
| 1137 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
| 1138 | struct gprs_nsvc *existing_nsvc; |
| 1139 | |
| 1140 | struct tlv_parsed tp; |
Jacob Erlbeck | b6390f9 | 2013-10-08 12:04:46 +0200 | [diff] [blame] | 1141 | uint16_t nsvci; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1142 | uint16_t nsei; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1143 | |
| 1144 | int rc; |
| 1145 | |
| 1146 | if (nsh->pdu_type == NS_PDUT_STATUS) { |
Jacob Erlbeck | 3d557b1 | 2013-10-28 13:29:11 +0100 | [diff] [blame] | 1147 | /* Do not respond, see 3GPP TS 08.16, 7.5.1 */ |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1148 | LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s " |
| 1149 | "for non-existing NS-VC\n", |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1150 | gprs_ns_ll_str(fallback_nsvc)); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1151 | return GPRS_NS_CS_SKIPPED; |
| 1152 | } |
| 1153 | |
Jacob Erlbeck | 3d557b1 | 2013-10-28 13:29:11 +0100 | [diff] [blame] | 1154 | if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) { |
| 1155 | /* Ignore this, see 3GPP TS 08.16, 7.4.1 */ |
| 1156 | LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s " |
| 1157 | "for non-existing NS-VC\n", |
| 1158 | gprs_ns_ll_str(fallback_nsvc)); |
| 1159 | return GPRS_NS_CS_SKIPPED; |
| 1160 | } |
| 1161 | |
| 1162 | if (nsh->pdu_type == NS_PDUT_RESET_ACK) { |
| 1163 | /* Ignore this, see 3GPP TS 08.16, 7.3.1 */ |
| 1164 | LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s " |
| 1165 | "for non-existing NS-VC\n", |
| 1166 | gprs_ns_ll_str(fallback_nsvc)); |
| 1167 | return GPRS_NS_CS_SKIPPED; |
| 1168 | } |
| 1169 | |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1170 | /* Only the RESET procedure creates a new NSVC */ |
| 1171 | if (nsh->pdu_type != NS_PDUT_RESET) { |
| 1172 | /* Since we have no NSVC, we have to use a fake */ |
| 1173 | log_set_context(GPRS_CTX_NSVC, fallback_nsvc); |
| 1174 | LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x " |
| 1175 | "from %s for non-existing NS-VC\n", |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1176 | nsh->pdu_type, gprs_ns_ll_str(fallback_nsvc)); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1177 | fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1178 | fallback_nsvc->nsvci_is_valid = 0; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1179 | fallback_nsvc->state = NSE_S_ALIVE; |
| 1180 | |
| 1181 | rc = gprs_ns_tx_status(fallback_nsvc, |
| 1182 | NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg); |
| 1183 | if (rc < 0) { |
| 1184 | LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1185 | rc, gprs_ns_ll_str(fallback_nsvc)); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1186 | return rc; |
| 1187 | } |
| 1188 | return GPRS_NS_CS_REJECTED; |
| 1189 | } |
| 1190 | |
| 1191 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 1192 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1193 | if (rc < 0) { |
| 1194 | LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during " |
| 1195 | "TLV Parse\n", rc); |
| 1196 | return rc; |
| 1197 | } |
| 1198 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 1199 | !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
| 1200 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 1201 | rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1202 | msg); |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 1203 | CHECK_TX_RC(rc, fallback_nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1204 | return -EINVAL; |
| 1205 | } |
Jacob Erlbeck | b6390f9 | 2013-10-08 12:04:46 +0200 | [diff] [blame] | 1206 | nsvci = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_VCI)); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1207 | nsei = ntohs(*(uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI)); |
Jacob Erlbeck | b6390f9 | 2013-10-08 12:04:46 +0200 | [diff] [blame] | 1208 | /* Check if we already know this NSVCI, the remote end might |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1209 | * simply have changed addresses, or it is a SGSN */ |
Jacob Erlbeck | b6390f9 | 2013-10-08 12:04:46 +0200 | [diff] [blame] | 1210 | existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1211 | if (!existing_nsvc) { |
| 1212 | *new_nsvc = gprs_nsvc_create(nsi, 0xffff); |
Jacob Erlbeck | 9b591b7 | 2013-11-11 09:43:05 +0100 | [diff] [blame] | 1213 | (*new_nsvc)->nsvci_is_valid = 0; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1214 | log_set_context(GPRS_CTX_NSVC, *new_nsvc); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1215 | gprs_ns_ll_copy(*new_nsvc, fallback_nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1216 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n", |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1217 | gprs_ns_ll_str(fallback_nsvc)); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1218 | |
| 1219 | return GPRS_NS_CS_CREATED; |
| 1220 | } |
| 1221 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1222 | /* Check NSEI */ |
| 1223 | if (existing_nsvc->nsei != nsei) { |
| 1224 | LOGP(DNS, LOGL_NOTICE, |
| 1225 | "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n", |
| 1226 | nsvci, existing_nsvc->nsei, nsei); |
| 1227 | |
| 1228 | /* Override old NSEI */ |
| 1229 | existing_nsvc->nsei = nsei; |
| 1230 | |
| 1231 | /* Do statistics */ |
| 1232 | rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]); |
| 1233 | } |
| 1234 | |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1235 | *new_nsvc = existing_nsvc; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1236 | gprs_ns_ll_copy(*new_nsvc, fallback_nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1237 | return GPRS_NS_CS_FOUND; |
| 1238 | } |
| 1239 | |
| 1240 | /*! \brief Process NS message independently from underlying transport layer |
| 1241 | * \param nsi NS instance to which the data belongs |
| 1242 | * \param[in] msg message buffer containing newly-received data |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1243 | * \param[inout] nsvc refers to the virtual connection, may be modified when |
| 1244 | * processing a NS_RESET |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1245 | * \returns 0 in case of success, < 0 in case of error |
| 1246 | * |
| 1247 | * This contains the main NS automaton. |
| 1248 | */ |
| 1249 | int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg, |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1250 | struct gprs_nsvc **nsvc) |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1251 | { |
| 1252 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 1253 | int rc = 0; |
| 1254 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1255 | msgb_nsei(msg) = (*nsvc)->nsei; |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 1256 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1257 | log_set_context(GPRS_CTX_NSVC, *nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 1258 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1259 | /* Increment number of Incoming bytes */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1260 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]); |
| 1261 | rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg)); |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1262 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1263 | switch (nsh->pdu_type) { |
| 1264 | case NS_PDUT_ALIVE: |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 1265 | /* If we're dead and blocked and suddenly receive a |
| 1266 | * NS-ALIVE out of the blue, we might have been re-started |
| 1267 | * and should send a NS-RESET to make sure everything recovers |
| 1268 | * fine. */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1269 | if ((*nsvc)->state == NSE_S_BLOCKED) |
Jacob Erlbeck | 6ac70a4 | 2014-10-07 14:12:30 +0200 | [diff] [blame] | 1270 | rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE); |
| 1271 | else if (!((*nsvc)->state & NSE_S_RESET)) |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1272 | rc = gprs_ns_tx_alive_ack(*nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1273 | break; |
| 1274 | case NS_PDUT_ALIVE_ACK: |
Harald Welte | 90af194 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 1275 | /* stop Tns-alive and start Tns-test */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1276 | nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST); |
| 1277 | if ((*nsvc)->remote_end_is_sgsn) { |
Harald Welte | 7fb7e61 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 1278 | /* FIXME: this should be one level higher */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1279 | if ((*nsvc)->state & NSE_S_BLOCKED) |
| 1280 | rc = gprs_ns_tx_unblock(*nsvc); |
Harald Welte | 7fb7e61 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 1281 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1282 | break; |
| 1283 | case NS_PDUT_UNITDATA: |
| 1284 | /* actual user data */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1285 | rc = gprs_ns_rx_unitdata(*nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1286 | break; |
| 1287 | case NS_PDUT_STATUS: |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1288 | rc = gprs_ns_rx_status(*nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1289 | break; |
| 1290 | case NS_PDUT_RESET: |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1291 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1292 | break; |
| 1293 | case NS_PDUT_RESET_ACK: |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1294 | rc = gprs_ns_rx_reset_ack(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1295 | break; |
| 1296 | case NS_PDUT_UNBLOCK: |
| 1297 | /* Section 7.2: unblocking procedure */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1298 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei); |
| 1299 | (*nsvc)->state &= ~NSE_S_BLOCKED; |
| 1300 | ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0); |
| 1301 | rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1302 | break; |
| 1303 | case NS_PDUT_UNBLOCK_ACK: |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1304 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 1305 | /* mark NS-VC as unblocked + active */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1306 | (*nsvc)->state = NSE_S_ALIVE; |
| 1307 | (*nsvc)->remote_state = NSE_S_ALIVE; |
| 1308 | ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1309 | break; |
| 1310 | case NS_PDUT_BLOCK: |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1311 | rc = gprs_ns_rx_block(*nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1312 | break; |
| 1313 | case NS_PDUT_BLOCK_ACK: |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1314 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1315 | /* mark remote NS-VC as blocked + active */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1316 | (*nsvc)->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1317 | break; |
| 1318 | default: |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 1319 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n", |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1320 | (*nsvc)->nsei, nsh->pdu_type); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1321 | rc = -EINVAL; |
| 1322 | break; |
| 1323 | } |
| 1324 | return rc; |
| 1325 | } |
| 1326 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1327 | /*! \brief Create a new GPRS NS instance |
| 1328 | * \param[in] cb Call-back function for incoming BSSGP data |
| 1329 | * \returns dynamically allocated gprs_ns_inst |
| 1330 | */ |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 1331 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1332 | { |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 1333 | struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1334 | |
| 1335 | nsi->cb = cb; |
| 1336 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 1337 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 1338 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 1339 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 1340 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 1341 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 1342 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 1343 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1344 | |
Harald Welte | 60cc6ac | 2010-05-13 14:20:56 +0200 | [diff] [blame] | 1345 | /* Create the dummy NSVC that we use for sending |
| 1346 | * messages to non-existant/unknown NS-VC's */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1347 | nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe); |
Jacob Erlbeck | 9b591b7 | 2013-11-11 09:43:05 +0100 | [diff] [blame] | 1348 | nsi->unknown_nsvc->nsvci_is_valid = 0; |
Harald Welte | 60cc6ac | 2010-05-13 14:20:56 +0200 | [diff] [blame] | 1349 | llist_del(&nsi->unknown_nsvc->list); |
Holger Hans Peter Freyther | 777b056 | 2014-07-07 20:00:35 +0200 | [diff] [blame] | 1350 | INIT_LLIST_HEAD(&nsi->unknown_nsvc->list); |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 1351 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1352 | return nsi; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 1355 | void gprs_ns_close(struct gprs_ns_inst *nsi) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1356 | { |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1357 | struct gprs_nsvc *nsvc, *nsvc2; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1358 | |
Holger Hans Peter Freyther | 777b056 | 2014-07-07 20:00:35 +0200 | [diff] [blame] | 1359 | gprs_nsvc_delete(nsi->unknown_nsvc); |
| 1360 | |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1361 | /* delete all NSVCs and clear their timers */ |
| 1362 | llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list) |
| 1363 | gprs_nsvc_delete(nsvc); |
| 1364 | |
| 1365 | /* close socket and unregister */ |
| 1366 | if (nsi->nsip.fd.data) { |
| 1367 | close(nsi->nsip.fd.fd); |
| 1368 | osmo_fd_unregister(&nsi->nsip.fd); |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 1369 | nsi->nsip.fd.data = NULL; |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1370 | } |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 1371 | } |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1372 | |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 1373 | /*! \brief Destroy an entire NS instance |
| 1374 | * \param nsi gprs_ns_inst that is to be destroyed |
| 1375 | * |
| 1376 | * This function releases all resources associated with the |
| 1377 | * NS-instance. |
| 1378 | */ |
| 1379 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 1380 | { |
| 1381 | gprs_ns_close(nsi); |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1382 | /* free the NSI */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1383 | talloc_free(nsi); |
| 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 1388 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 1389 | |
| 1390 | /* Read a single NS-over-IP message */ |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1391 | static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error, |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1392 | struct sockaddr_in *saddr) |
| 1393 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 1394 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1395 | int ret = 0; |
| 1396 | socklen_t saddr_len = sizeof(*saddr); |
| 1397 | |
| 1398 | if (!msg) { |
| 1399 | *error = -ENOMEM; |
| 1400 | return NULL; |
| 1401 | } |
| 1402 | |
Holger Hans Peter Freyther | 26c3251 | 2010-05-28 03:25:36 +0800 | [diff] [blame] | 1403 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0, |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1404 | (struct sockaddr *)saddr, &saddr_len); |
| 1405 | if (ret < 0) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 1406 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 1407 | strerror(errno)); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1408 | msgb_free(msg); |
| 1409 | *error = ret; |
| 1410 | return NULL; |
| 1411 | } else if (ret == 0) { |
| 1412 | msgb_free(msg); |
| 1413 | *error = ret; |
| 1414 | return NULL; |
| 1415 | } |
| 1416 | |
| 1417 | msg->l2h = msg->data; |
| 1418 | msgb_put(msg, ret); |
| 1419 | |
| 1420 | return msg; |
| 1421 | } |
| 1422 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1423 | static int handle_nsip_read(struct osmo_fd *bfd) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1424 | { |
| 1425 | int error; |
| 1426 | struct sockaddr_in saddr; |
| 1427 | struct gprs_ns_inst *nsi = bfd->data; |
| 1428 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 1429 | |
| 1430 | if (!msg) |
| 1431 | return error; |
| 1432 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 1433 | error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP); |
Harald Welte | 91813cf | 2010-05-12 18:38:45 +0000 | [diff] [blame] | 1434 | |
| 1435 | msgb_free(msg); |
| 1436 | |
| 1437 | return error; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1440 | static int handle_nsip_write(struct osmo_fd *bfd) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1441 | { |
| 1442 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 1443 | return -EIO; |
| 1444 | } |
| 1445 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 1446 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1447 | { |
| 1448 | int rc; |
| 1449 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 1450 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 1451 | |
| 1452 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 1453 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 1454 | |
Holger Hans Peter Freyther | 5274646 | 2012-03-01 20:30:32 +0100 | [diff] [blame] | 1455 | msgb_free(msg); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1456 | |
| 1457 | return rc; |
| 1458 | } |
| 1459 | |
| 1460 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1461 | static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1462 | { |
| 1463 | int rc = 0; |
| 1464 | |
| 1465 | if (what & BSC_FD_READ) |
| 1466 | rc = handle_nsip_read(bfd); |
| 1467 | if (what & BSC_FD_WRITE) |
| 1468 | rc = handle_nsip_write(bfd); |
| 1469 | |
| 1470 | return rc; |
| 1471 | } |
| 1472 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1473 | /*! \brief Create a listening socket for GPRS NS/UDP/IP |
| 1474 | * \param[in] nsi NS protocol instance to listen |
| 1475 | * \returns >=0 (fd) in case of success, negative in case of error |
| 1476 | * |
| 1477 | * A call to this function will create a UDP socket bound to the port |
| 1478 | * number and IP address specified in the NS protocol instance. The |
| 1479 | * file descriptor of the socket will be stored in nsi->nsip.fd. |
| 1480 | */ |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 1481 | int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1482 | { |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 1483 | struct in_addr in; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1484 | int ret; |
| 1485 | |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 1486 | in.s_addr = htonl(nsi->nsip.local_ip); |
| 1487 | |
| 1488 | nsi->nsip.fd.cb = nsip_fd_cb; |
| 1489 | nsi->nsip.fd.data = nsi; |
| 1490 | ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM, |
| 1491 | IPPROTO_UDP, inet_ntoa(in), |
| 1492 | nsi->nsip.local_port, OSMO_SOCK_F_BIND); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1493 | if (ret < 0) |
| 1494 | return ret; |
| 1495 | |
Holger Hans Peter Freyther | 2c3393d | 2013-03-25 11:59:58 +0100 | [diff] [blame] | 1496 | ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS, |
| 1497 | &nsi->nsip.dscp, sizeof(nsi->nsip.dscp)); |
| 1498 | if (ret < 0) |
| 1499 | LOGP(DNS, LOGL_ERROR, |
| 1500 | "Failed to set the DSCP to %d with ret(%d) errno(%d)\n", |
| 1501 | nsi->nsip.dscp, ret, errno); |
| 1502 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1503 | |
| 1504 | return ret; |
| 1505 | } |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1506 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1507 | /*! \brief Initiate a RESET procedure |
| 1508 | * \param[in] nsvc NS-VC in which to start the procedure |
| 1509 | * \param[in] cause Numeric NS cause value |
| 1510 | * |
| 1511 | * This is a high-level function initiating a NS-RESET procedure. It |
| 1512 | * will not only send a NS-RESET, but also set the state to BLOCKED and |
| 1513 | * start the Tns-reset timer. |
| 1514 | */ |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 1515 | int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1516 | { |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 1517 | int rc; |
| 1518 | |
Harald Welte | b1020d5 | 2010-05-25 22:17:30 +0200 | [diff] [blame] | 1519 | LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n", |
| 1520 | nsvc->nsei); |
| 1521 | |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1522 | /* Mark NS-VC locally as blocked and dead */ |
Jacob Erlbeck | 6ac70a4 | 2014-10-07 14:12:30 +0200 | [diff] [blame] | 1523 | nsvc->state = NSE_S_BLOCKED | NSE_S_RESET; |
| 1524 | |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1525 | /* Send NS-RESET PDU */ |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 1526 | rc = gprs_ns_tx_reset(nsvc, cause); |
| 1527 | if (rc < 0) { |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1528 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
| 1529 | nsvc->nsei); |
| 1530 | } |
| 1531 | /* Start Tns-reset */ |
| 1532 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 1533 | |
| 1534 | return rc; |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1537 | /*! \brief Establish a NS connection (from the BSS) to the SGSN |
| 1538 | * \param nsi NS-instance |
| 1539 | * \param[in] dest Destination IP/Port |
| 1540 | * \param[in] nsei NSEI of the to-be-established NS-VC |
| 1541 | * \param[in] nsvci NSVCI of the to-be-established NS-VC |
| 1542 | * \returns struct gprs_nsvc representing the new NS-VC |
| 1543 | * |
| 1544 | * This function will establish a single NS/UDP/IP connection in uplink |
| 1545 | * (BSS to SGSN) direction. |
| 1546 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1547 | struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 1548 | struct sockaddr_in *dest, uint16_t nsei, |
| 1549 | uint16_t nsvci) |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1550 | { |
| 1551 | struct gprs_nsvc *nsvc; |
| 1552 | |
| 1553 | nsvc = nsvc_by_rem_addr(nsi, dest); |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 1554 | if (!nsvc) |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1555 | nsvc = gprs_nsvc_create(nsi, nsvci); |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 1556 | nsvc->ip.bts_addr = *dest; |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 1557 | nsvc->nsei = nsei; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1558 | nsvc->remote_end_is_sgsn = 1; |
| 1559 | |
Holger Hans Peter Freyther | 5617d99 | 2010-05-23 21:18:01 +0800 | [diff] [blame] | 1560 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 1561 | return nsvc; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1562 | } |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1563 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 1564 | void gprs_ns_set_log_ss(int ss) |
| 1565 | { |
| 1566 | DNS = ss; |
| 1567 | } |
| 1568 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1569 | /*! }@ */ |