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