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