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, |
| 103 | }; |
| 104 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 105 | static const struct rate_ctr_desc nsvc_ctr_description[] = { |
| 106 | { "packets.in", "Packets at NS Level ( In)" }, |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 107 | { "packets.out","Packets at NS Level (Out)" }, |
| 108 | { "bytes.in", "Bytes at NS Level ( In)" }, |
| 109 | { "bytes.out", "Bytes at NS Level (Out)" }, |
| 110 | { "blocked", "NS-VC Block count " }, |
| 111 | { "dead", "NS-VC gone dead count " }, |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static const struct rate_ctr_group_desc nsvc_ctrg_desc = { |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 115 | .group_name_prefix = "ns.nsvc", |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 116 | .group_description = "NSVC Peer Statistics", |
| 117 | .num_ctr = ARRAY_SIZE(nsvc_ctr_description), |
| 118 | .ctr_desc = nsvc_ctr_description, |
| 119 | }; |
| 120 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 121 | /*! \brief Lookup struct gprs_nsvc based on NSVCI |
| 122 | * \param[in] nsi NS instance in which to search |
| 123 | * \param[in] nsvci NSVCI to be searched |
| 124 | * \returns gprs_nsvc of respective NSVCI |
| 125 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 126 | 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] | 127 | { |
| 128 | struct gprs_nsvc *nsvc; |
| 129 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 130 | if (nsvc->nsvci == nsvci) |
| 131 | return nsvc; |
| 132 | } |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 136 | /*! \brief Lookup struct gprs_nsvc based on NSEI |
| 137 | * \param[in] nsi NS instance in which to search |
| 138 | * \param[in] nsei NSEI to be searched |
| 139 | * \returns gprs_nsvc of respective NSEI |
| 140 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 141 | 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] | 142 | { |
| 143 | struct gprs_nsvc *nsvc; |
| 144 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 145 | if (nsvc->nsei == nsei) |
| 146 | return nsvc; |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 151 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 152 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 153 | struct sockaddr_in *sin) |
| 154 | { |
| 155 | struct gprs_nsvc *nsvc; |
| 156 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 157 | if (nsvc->ip.bts_addr.sin_addr.s_addr == |
| 158 | sin->sin_addr.s_addr && |
| 159 | nsvc->ip.bts_addr.sin_port == sin->sin_port) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 160 | return nsvc; |
| 161 | } |
| 162 | return NULL; |
| 163 | } |
| 164 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 165 | static void gprs_ns_timer_cb(void *data); |
| 166 | |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 167 | 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] | 168 | { |
| 169 | struct gprs_nsvc *nsvc; |
| 170 | |
Harald Welte | b1020d5 | 2010-05-25 22:17:30 +0200 | [diff] [blame] | 171 | LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci); |
| 172 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 173 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 174 | nsvc->nsvci = nsvci; |
| 175 | /* before RESET procedure: BLOCKED and DEAD */ |
| 176 | nsvc->state = NSE_S_BLOCKED; |
| 177 | nsvc->nsi = nsi; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 178 | nsvc->timer.cb = gprs_ns_timer_cb; |
| 179 | nsvc->timer.data = nsvc; |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 180 | nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 181 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 182 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 183 | |
| 184 | return nsvc; |
| 185 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 186 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 187 | /*! \brief Delete given NS-VC |
| 188 | * \param[in] nsvc gprs_nsvc to be deleted |
| 189 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 190 | void gprs_nsvc_delete(struct gprs_nsvc *nsvc) |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 191 | { |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 192 | if (osmo_timer_pending(&nsvc->timer)) |
| 193 | osmo_timer_del(&nsvc->timer); |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 194 | llist_del(&nsvc->list); |
| 195 | talloc_free(nsvc); |
| 196 | } |
| 197 | |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 198 | 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] | 199 | uint8_t cause) |
| 200 | { |
| 201 | struct ns_signal_data nssd; |
| 202 | |
| 203 | nssd.nsvc = nsvc; |
| 204 | nssd.cause = cause; |
| 205 | |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 206 | osmo_signal_dispatch(SS_L_NS, signal, &nssd); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 209 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 210 | static const struct value_string ns_cause_str[] = { |
| 211 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 212 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 213 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 214 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 215 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 216 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 217 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 218 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 219 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 220 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 221 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 222 | { 0, NULL } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 223 | }; |
| 224 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 225 | /*! \brief Obtain a human-readable string for NS cause value */ |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 226 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 227 | { |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 228 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 231 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 232 | 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] | 233 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 234 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 235 | { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 236 | int ret; |
| 237 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 238 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 239 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 240 | /* Increment number of Uplink bytes */ |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 241 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]); |
| 242 | 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] | 243 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 244 | switch (nsvc->ll) { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 245 | case GPRS_NS_LL_UDP: |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 246 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 247 | break; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 248 | case GPRS_NS_LL_FR_GRE: |
| 249 | ret = gprs_ns_frgre_sendmsg(nsvc, msg); |
| 250 | break; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 251 | default: |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 252 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 253 | msgb_free(msg); |
| 254 | ret = -EIO; |
| 255 | break; |
| 256 | } |
| 257 | return ret; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 258 | } |
| 259 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 260 | 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] | 261 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 262 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 263 | struct gprs_ns_hdr *nsh; |
| 264 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 265 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 266 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 267 | if (!msg) |
| 268 | return -ENOMEM; |
| 269 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 270 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 271 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 272 | |
| 273 | nsh->pdu_type = pdu_type; |
| 274 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 275 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 276 | } |
| 277 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 278 | /*! \brief Transmit a NS-RESET on a given NSVC |
| 279 | * \param[in] nsvc NS-VC used for transmission |
| 280 | * \paam[in] cause Numeric NS cause value |
| 281 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 282 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 283 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 284 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 285 | struct gprs_ns_hdr *nsh; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 286 | uint16_t nsvci = htons(nsvc->nsvci); |
| 287 | uint16_t nsei = htons(nsvc->nsei); |
| 288 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 289 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 290 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 291 | if (!msg) |
| 292 | return -ENOMEM; |
| 293 | |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 294 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n", |
| 295 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 296 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 297 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 298 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 299 | nsh->pdu_type = NS_PDUT_RESET; |
| 300 | |
| 301 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 302 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 303 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 304 | |
| 305 | return gprs_ns_tx(nsvc, msg); |
| 306 | |
| 307 | } |
| 308 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 309 | /*! \brief Transmit a NS-STATUS on a given NSVC |
| 310 | * \param[in] nsvc NS-VC to be used for transmission |
| 311 | * \param[in] cause Numeric NS cause value |
| 312 | * \param[in] bvci BVCI to be reset within NSVC |
| 313 | * \param[in] orig_msg message causing the STATUS */ |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 314 | int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause, |
| 315 | uint16_t bvci, struct msgb *orig_msg) |
| 316 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 317 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 318 | struct gprs_ns_hdr *nsh; |
| 319 | uint16_t nsvci = htons(nsvc->nsvci); |
| 320 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 321 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 322 | |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 323 | bvci = htons(bvci); |
| 324 | |
| 325 | if (!msg) |
| 326 | return -ENOMEM; |
| 327 | |
Harald Welte | 90af194 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 328 | 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] | 329 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 330 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 331 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 332 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 333 | nsh->pdu_type = NS_PDUT_STATUS; |
| 334 | |
| 335 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 336 | |
| 337 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 338 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 339 | cause == NS_CAUSE_NSVC_UNKNOWN) |
| 340 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 341 | |
| 342 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 343 | switch (cause) { |
| 344 | case NS_CAUSE_SEM_INCORR_PDU: |
| 345 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 346 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 347 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 348 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 349 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 350 | orig_msg->l2h); |
| 351 | break; |
| 352 | default: |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | /* Section 9.2.7.3: Static conditions for BVCI */ |
| 357 | if (cause == NS_CAUSE_BVCI_UNKNOWN) |
| 358 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci); |
| 359 | |
| 360 | return gprs_ns_tx(nsvc, msg); |
| 361 | } |
| 362 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 363 | /*! \brief Transmit a NS-BLOCK on a tiven NS-VC |
| 364 | * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted |
| 365 | * \param[in] cause Numeric NS Cause value |
| 366 | * \returns 0 in case of success |
| 367 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 368 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause) |
| 369 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 370 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 371 | struct gprs_ns_hdr *nsh; |
| 372 | uint16_t nsvci = htons(nsvc->nsvci); |
| 373 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 374 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 375 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 376 | if (!msg) |
| 377 | return -ENOMEM; |
| 378 | |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 379 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n", |
| 380 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 381 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 382 | /* be conservative and mark it as blocked even now! */ |
| 383 | nsvc->state |= NSE_S_BLOCKED; |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 384 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 385 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 386 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 387 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 388 | nsh->pdu_type = NS_PDUT_BLOCK; |
| 389 | |
| 390 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 391 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 392 | |
| 393 | return gprs_ns_tx(nsvc, msg); |
| 394 | } |
| 395 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 396 | /*! \brief Transmit a NS-UNBLOCK on a given NS-VC |
| 397 | * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted |
| 398 | * \returns 0 in case of success |
| 399 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 400 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc) |
| 401 | { |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 402 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 403 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n", |
| 404 | nsvc->nsei, nsvc->nsvci); |
| 405 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 406 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 407 | } |
| 408 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 409 | /*! \brief Transmit a NS-ALIVE on a given NS-VC |
| 410 | * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted |
| 411 | * \returns 0 in case of success |
| 412 | */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 413 | int gprs_ns_tx_alive(struct gprs_nsvc *nsvc) |
| 414 | { |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 415 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 416 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n", |
| 417 | nsvc->nsei, nsvc->nsvci); |
| 418 | |
| 419 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 420 | } |
| 421 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 422 | /*! \brief Transmit a NS-ALIVE-ACK on a given NS-VC |
| 423 | * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted |
| 424 | * \returns 0 in case of success |
| 425 | */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 426 | int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc) |
| 427 | { |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 428 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 429 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n", |
| 430 | nsvc->nsei, nsvc->nsvci); |
| 431 | |
| 432 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
| 433 | } |
| 434 | |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 435 | static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = { |
| 436 | [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET, |
| 437 | [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE, |
| 438 | [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST, |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 439 | }; |
| 440 | |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 441 | static const struct value_string timer_mode_strs[] = { |
| 442 | { NSVC_TIMER_TNS_RESET, "tns-reset" }, |
| 443 | { NSVC_TIMER_TNS_ALIVE, "tns-alive" }, |
| 444 | { NSVC_TIMER_TNS_TEST, "tns-test" }, |
| 445 | { 0, NULL } |
| 446 | }; |
| 447 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 448 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 449 | { |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 450 | enum ns_timeout tout = timer_mode_tout[mode]; |
| 451 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
| 452 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 453 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 4e187c6 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 454 | DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n", |
| 455 | nsvc->nsei, get_value_string(timer_mode_strs, mode), |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 456 | seconds); |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 457 | |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 458 | if (osmo_timer_pending(&nsvc->timer)) |
| 459 | osmo_timer_del(&nsvc->timer); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 460 | |
| 461 | nsvc->timer_mode = mode; |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 462 | osmo_timer_schedule(&nsvc->timer, seconds, 0); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 465 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 466 | { |
| 467 | struct gprs_nsvc *nsvc = data; |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 468 | enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode]; |
| 469 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 470 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 471 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 4e187c6 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 472 | DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n", |
| 473 | nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode), |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 474 | seconds); |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 475 | |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 476 | switch (nsvc->timer_mode) { |
| 477 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 478 | /* Tns-alive case: we expired without response ! */ |
| 479 | nsvc->alive_retries++; |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 480 | if (nsvc->alive_retries > |
| 481 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 482 | /* mark as dead and blocked */ |
| 483 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 484 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
| 485 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]); |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 486 | LOGP(DNS, LOGL_NOTICE, |
| 487 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 488 | "%u times, blocking NS-VC\n", nsvc->nsei, |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 489 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]); |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 490 | ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0); |
| 491 | ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 492 | return; |
| 493 | } |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 494 | /* Tns-test case: send NS-ALIVE PDU */ |
| 495 | gprs_ns_tx_alive(nsvc); |
| 496 | /* start Tns-alive timer */ |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 497 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 498 | break; |
| 499 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 500 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 501 | gprs_ns_tx_alive(nsvc); |
| 502 | /* start Tns-alive timer (transition into faster |
| 503 | * alive retransmissions) */ |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 504 | nsvc->alive_retries = 0; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 505 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 506 | break; |
| 507 | case NSVC_TIMER_TNS_RESET: |
| 508 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 570fb83 | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 509 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 510 | /* Re-start Tns-reset timer */ |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 511 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 512 | break; |
Harald Welte | 2fc725d | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 513 | case _NSVC_TIMER_NR: |
| 514 | break; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 515 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* Section 9.2.6 */ |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 519 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 520 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 521 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 522 | struct gprs_ns_hdr *nsh; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 523 | uint16_t nsvci, nsei; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 524 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 525 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 526 | if (!msg) |
| 527 | return -ENOMEM; |
| 528 | |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 529 | nsvci = htons(nsvc->nsvci); |
| 530 | nsei = htons(nsvc->nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 531 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 532 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 533 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 534 | |
| 535 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 536 | |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 537 | 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] | 538 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 539 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 540 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 541 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 542 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 543 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 544 | } |
| 545 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 546 | /*! \brief High-level function for transmitting a NS-UNITDATA messsage |
| 547 | * \param[in] nsi NS-instance on which we shall transmit |
| 548 | * \param[in] msg struct msgb to be trasnmitted |
| 549 | * |
| 550 | * This function obtains the NS-VC by the msgb_nsei(msg) and then checks |
| 551 | * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS |
| 552 | * header for the NS-UNITDATA message type and sends it off. |
| 553 | * |
| 554 | * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive |
| 555 | */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 556 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 557 | { |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 558 | struct gprs_nsvc *nsvc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 559 | struct gprs_ns_hdr *nsh; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 560 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 561 | |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 562 | nsvc = gprs_nsvc_by_nsei(nsi, msgb_nsei(msg)); |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 563 | if (!nsvc) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 564 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 565 | "to NS-VC!\n", msgb_nsei(msg)); |
Harald Welte | f47df64 | 2010-08-09 21:15:40 +0800 | [diff] [blame] | 566 | msgb_free(msg); |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 567 | return -EINVAL; |
| 568 | } |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 569 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 570 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 571 | if (!(nsvc->state & NSE_S_ALIVE)) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 572 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n", |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 573 | nsvc->nsei); |
Harald Welte | f47df64 | 2010-08-09 21:15:40 +0800 | [diff] [blame] | 574 | msgb_free(msg); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 575 | return -EBUSY; |
| 576 | } |
| 577 | if (nsvc->state & NSE_S_BLOCKED) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 578 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n", |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 579 | nsvc->nsei); |
Harald Welte | f47df64 | 2010-08-09 21:15:40 +0800 | [diff] [blame] | 580 | msgb_free(msg); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 581 | return -EBUSY; |
| 582 | } |
| 583 | |
Harald Welte | ec20ba4 | 2010-05-13 12:18:49 +0200 | [diff] [blame] | 584 | msg->l2h = msgb_push(msg, sizeof(*nsh) + 3); |
| 585 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 586 | if (!nsh) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 587 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | f47df64 | 2010-08-09 21:15:40 +0800 | [diff] [blame] | 588 | msgb_free(msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 589 | return -EIO; |
| 590 | } |
| 591 | |
| 592 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 593 | /* spare octet in data[0] */ |
| 594 | nsh->data[1] = bvci >> 8; |
| 595 | nsh->data[2] = bvci & 0xff; |
| 596 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 597 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* Section 9.2.10: receive side */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 601 | 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] | 602 | { |
| 603 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 604 | uint16_t bvci; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 605 | |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 606 | if (nsvc->state & NSE_S_BLOCKED) |
| 607 | return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED, |
| 608 | 0, msg); |
| 609 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 610 | /* spare octet in data[0] */ |
| 611 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | ec19c10 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 612 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 613 | msgb_bvci(msg) = bvci; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 614 | |
| 615 | /* call upper layer (BSSGP) */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 616 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* Section 9.2.7 */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 620 | 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] | 621 | { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 622 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 623 | struct tlv_parsed tp; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 624 | uint8_t cause; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 625 | int rc; |
| 626 | |
Harald Welte | 4133783 | 2010-05-16 00:24:26 +0200 | [diff] [blame] | 627 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 628 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 629 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 630 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 631 | if (rc < 0) { |
| 632 | LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n"); |
| 633 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: " |
| 634 | "Error during TLV Parse\n", nsvc->nsei); |
| 635 | return rc; |
| 636 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 637 | |
| 638 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 639 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 640 | return -EINVAL; |
| 641 | } |
| 642 | |
| 643 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | 4133783 | 2010-05-16 00:24:26 +0200 | [diff] [blame] | 644 | LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* Section 7.3 */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 650 | 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] | 651 | { |
| 652 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 653 | struct tlv_parsed tp; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 654 | uint8_t *cause; |
| 655 | uint16_t *nsvci, *nsei; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 656 | int rc; |
| 657 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 658 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 659 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 660 | if (rc < 0) { |
| 661 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET " |
| 662 | "Error during TLV Parse\n", nsvc->nsei); |
| 663 | return rc; |
| 664 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 665 | |
| 666 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 667 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 668 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 669 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 670 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 671 | return -EINVAL; |
| 672 | } |
| 673 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 674 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 675 | nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 676 | nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 677 | |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 678 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n", |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 679 | nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause)); |
| 680 | |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 681 | /* Mark NS-VC as blocked and alive */ |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 682 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | e8b9ca2 | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 683 | |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 684 | nsvc->nsei = ntohs(*nsei); |
| 685 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 686 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 687 | /* start the test procedure */ |
Harald Welte | c953120 | 2010-05-28 09:58:27 +0200 | [diff] [blame] | 688 | gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 689 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 690 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 691 | /* inform interested parties about the fact that this NSVC |
| 692 | * has received RESET */ |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 693 | ns_osmo_signal_dispatch(nsvc, S_NS_RESET, *cause); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 694 | |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 695 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 696 | } |
| 697 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 698 | static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 699 | { |
| 700 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 701 | struct tlv_parsed tp; |
| 702 | uint8_t *cause; |
| 703 | int rc; |
| 704 | |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 705 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 706 | |
| 707 | nsvc->state |= NSE_S_BLOCKED; |
| 708 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 709 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 710 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 711 | if (rc < 0) { |
| 712 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK " |
| 713 | "Error during TLV Parse\n", nsvc->nsei); |
| 714 | return rc; |
| 715 | } |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 716 | |
| 717 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 718 | !TLVP_PRESENT(&tp, NS_IE_VCI)) { |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 719 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 720 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 721 | return -EINVAL; |
| 722 | } |
| 723 | |
| 724 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 725 | //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 726 | |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 727 | ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 728 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 729 | |
| 730 | return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK); |
| 731 | } |
| 732 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 733 | /*! \brief Receive incoming NS message from underlying transport layer |
| 734 | * \param nsi NS instance to which the data belongs |
| 735 | * \param[in] msg message buffer containing newly-received data |
| 736 | * \param[in] saddr socketaddr from which data was received |
| 737 | * \param[in] ll link-layer type in which data was received |
| 738 | * \returns 0 in case of success, < 0 in case of error |
| 739 | * |
| 740 | * This is the main entry point int othe NS imlementation where frames |
| 741 | * from the underlying transport (normally UDP) enter. |
| 742 | */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 743 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 744 | struct sockaddr_in *saddr, enum gprs_ns_ll ll) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 745 | { |
| 746 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 747 | struct gprs_nsvc *nsvc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 748 | int rc = 0; |
| 749 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 750 | /* look up the NSVC based on source address */ |
| 751 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 752 | if (!nsvc) { |
Harald Welte | 9a39293 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 753 | struct tlv_parsed tp; |
| 754 | uint16_t nsei; |
Harald Welte | 1100a9d | 2010-05-19 16:01:39 +0200 | [diff] [blame] | 755 | if (nsh->pdu_type == NS_PDUT_STATUS) { |
| 756 | LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s:%u " |
| 757 | "for non-existing NS-VC\n", |
| 758 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
| 759 | return 0; |
| 760 | } |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 761 | /* Only the RESET procedure creates a new NSVC */ |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 762 | if (nsh->pdu_type != NS_PDUT_RESET) { |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 763 | /* Since we have no NSVC, we have to use a fake */ |
| 764 | nsvc = nsi->unknown_nsvc; |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 765 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 766 | LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x " |
Harald Welte | bba902d | 2010-05-11 18:38:36 +0200 | [diff] [blame] | 767 | "from %s:%u for non-existing NS-VC\n", |
| 768 | nsh->pdu_type, inet_ntoa(saddr->sin_addr), |
| 769 | ntohs(saddr->sin_port)); |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 770 | nsvc->nsvci = nsvc->nsei = 0xfffe; |
| 771 | nsvc->ip.bts_addr = *saddr; |
| 772 | nsvc->state = NSE_S_ALIVE; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 773 | nsvc->ll = ll; |
Harald Welte | e0c42d1 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 774 | #if 0 |
| 775 | return gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE); |
| 776 | #else |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 777 | return gprs_ns_tx_status(nsvc, |
Harald Welte | 803647e | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 778 | NS_CAUSE_PDU_INCOMP_PSTATE, 0, |
| 779 | msg); |
Harald Welte | e0c42d1 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 780 | #endif |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 781 | } |
Harald Welte | 9a39293 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 782 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 783 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 784 | if (rc < 0) { |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 785 | LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during " |
| 786 | "TLV Parse\n", rc); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 787 | return rc; |
| 788 | } |
Harald Welte | 9a39293 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 789 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 790 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 791 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | 9a39293 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 792 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 793 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
| 794 | msg); |
Harald Welte | 9a39293 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 795 | return -EINVAL; |
| 796 | } |
| 797 | nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI)); |
| 798 | /* Check if we already know this NSEI, the remote end might |
| 799 | * simply have changed addresses, or it is a SGSN */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 800 | nsvc = gprs_nsvc_by_nsei(nsi, nsei); |
Harald Welte | 9a39293 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 801 | if (!nsvc) { |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 802 | nsvc = gprs_nsvc_create(nsi, 0xffff); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 803 | nsvc->ll = ll; |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 804 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 9a39293 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 805 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n", |
| 806 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
Harald Welte | 9a39293 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 807 | } |
Harald Welte | 2b7d465 | 2010-05-11 18:40:45 +0200 | [diff] [blame] | 808 | /* Update the remote peer IP address/port */ |
| 809 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 810 | } else |
| 811 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 812 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 813 | log_set_context(GPRS_CTX_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 814 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 815 | /* Increment number of Incoming bytes */ |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 816 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]); |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 817 | 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] | 818 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 819 | switch (nsh->pdu_type) { |
| 820 | case NS_PDUT_ALIVE: |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 821 | /* If we're dead and blocked and suddenly receive a |
| 822 | * NS-ALIVE out of the blue, we might have been re-started |
| 823 | * and should send a NS-RESET to make sure everything recovers |
| 824 | * fine. */ |
| 825 | if (nsvc->state == NSE_S_BLOCKED) |
| 826 | rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE); |
| 827 | else |
| 828 | rc = gprs_ns_tx_alive_ack(nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 829 | break; |
| 830 | case NS_PDUT_ALIVE_ACK: |
Harald Welte | 90af194 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 831 | /* stop Tns-alive and start Tns-test */ |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 832 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 7fb7e61 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 833 | if (nsvc->remote_end_is_sgsn) { |
| 834 | /* FIXME: this should be one level higher */ |
| 835 | if (nsvc->state & NSE_S_BLOCKED) |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 836 | rc = gprs_ns_tx_unblock(nsvc); |
Harald Welte | 7fb7e61 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 837 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 838 | break; |
| 839 | case NS_PDUT_UNITDATA: |
| 840 | /* actual user data */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 841 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 842 | break; |
| 843 | case NS_PDUT_STATUS: |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 844 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 845 | break; |
| 846 | case NS_PDUT_RESET: |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 847 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 848 | break; |
| 849 | case NS_PDUT_RESET_ACK: |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 850 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 851 | /* mark NS-VC as blocked + active */ |
| 852 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 853 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 854 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 855 | if (nsvc->persistent || nsvc->remote_end_is_sgsn) { |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 856 | /* stop RESET timer */ |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 857 | osmo_timer_del(&nsvc->timer); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 858 | } |
Harald Welte | 90af194 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 859 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
| 860 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
Harald Welte | c953120 | 2010-05-28 09:58:27 +0200 | [diff] [blame] | 861 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 862 | break; |
| 863 | case NS_PDUT_UNBLOCK: |
| 864 | /* Section 7.2: unblocking procedure */ |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 865 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 866 | nsvc->state &= ~NSE_S_BLOCKED; |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 867 | ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 868 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 869 | break; |
| 870 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 871 | 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] | 872 | /* mark NS-VC as unblocked + active */ |
| 873 | nsvc->state = NSE_S_ALIVE; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 874 | nsvc->remote_state = NSE_S_ALIVE; |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 875 | ns_osmo_signal_dispatch(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 876 | break; |
| 877 | case NS_PDUT_BLOCK: |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 878 | rc = gprs_ns_rx_block(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 879 | break; |
| 880 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 881 | 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] | 882 | /* mark remote NS-VC as blocked + active */ |
| 883 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 884 | break; |
| 885 | default: |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 886 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n", |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 887 | nsvc->nsei, nsh->pdu_type); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 888 | rc = -EINVAL; |
| 889 | break; |
| 890 | } |
| 891 | return rc; |
| 892 | } |
| 893 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 894 | /*! \brief Create a new GPRS NS instance |
| 895 | * \param[in] cb Call-back function for incoming BSSGP data |
| 896 | * \returns dynamically allocated gprs_ns_inst |
| 897 | */ |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 898 | 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] | 899 | { |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 900 | struct gprs_ns_inst *nsi = talloc_zero(ctx, struct gprs_ns_inst); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 901 | |
| 902 | nsi->cb = cb; |
| 903 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 904 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 905 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 906 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 907 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 908 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 909 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 910 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 911 | |
Harald Welte | 60cc6ac | 2010-05-13 14:20:56 +0200 | [diff] [blame] | 912 | /* Create the dummy NSVC that we use for sending |
| 913 | * messages to non-existant/unknown NS-VC's */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 914 | nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe); |
Harald Welte | 60cc6ac | 2010-05-13 14:20:56 +0200 | [diff] [blame] | 915 | llist_del(&nsi->unknown_nsvc->list); |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 916 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 917 | return nsi; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 918 | } |
| 919 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 920 | /*! \brief Destroy an entire NS instance |
| 921 | * \param nsi gprs_ns_inst that is to be destroyed |
| 922 | * |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 923 | * This function releases all resources associated with the |
| 924 | * NS-instance. |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 925 | */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 926 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 927 | { |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 928 | struct gprs_nsvc *nsvc, *nsvc2; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 929 | |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 930 | /* delete all NSVCs and clear their timers */ |
| 931 | llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list) |
| 932 | gprs_nsvc_delete(nsvc); |
| 933 | |
| 934 | /* close socket and unregister */ |
| 935 | if (nsi->nsip.fd.data) { |
| 936 | close(nsi->nsip.fd.fd); |
| 937 | osmo_fd_unregister(&nsi->nsip.fd); |
| 938 | } |
| 939 | |
| 940 | /* free the NSI */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 941 | talloc_free(nsi); |
| 942 | } |
| 943 | |
| 944 | |
| 945 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 946 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 947 | |
| 948 | /* Read a single NS-over-IP message */ |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 949 | static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error, |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 950 | struct sockaddr_in *saddr) |
| 951 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 952 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 953 | int ret = 0; |
| 954 | socklen_t saddr_len = sizeof(*saddr); |
| 955 | |
| 956 | if (!msg) { |
| 957 | *error = -ENOMEM; |
| 958 | return NULL; |
| 959 | } |
| 960 | |
Holger Hans Peter Freyther | 26c3251 | 2010-05-28 03:25:36 +0800 | [diff] [blame] | 961 | 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] | 962 | (struct sockaddr *)saddr, &saddr_len); |
| 963 | if (ret < 0) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 964 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 965 | strerror(errno)); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 966 | msgb_free(msg); |
| 967 | *error = ret; |
| 968 | return NULL; |
| 969 | } else if (ret == 0) { |
| 970 | msgb_free(msg); |
| 971 | *error = ret; |
| 972 | return NULL; |
| 973 | } |
| 974 | |
| 975 | msg->l2h = msg->data; |
| 976 | msgb_put(msg, ret); |
| 977 | |
| 978 | return msg; |
| 979 | } |
| 980 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 981 | static int handle_nsip_read(struct osmo_fd *bfd) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 982 | { |
| 983 | int error; |
| 984 | struct sockaddr_in saddr; |
| 985 | struct gprs_ns_inst *nsi = bfd->data; |
| 986 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 987 | |
| 988 | if (!msg) |
| 989 | return error; |
| 990 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 991 | error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP); |
Harald Welte | 91813cf | 2010-05-12 18:38:45 +0000 | [diff] [blame] | 992 | |
| 993 | msgb_free(msg); |
| 994 | |
| 995 | return error; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 998 | static int handle_nsip_write(struct osmo_fd *bfd) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 999 | { |
| 1000 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 1001 | return -EIO; |
| 1002 | } |
| 1003 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 1004 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1005 | { |
| 1006 | int rc; |
| 1007 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 1008 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 1009 | |
| 1010 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 1011 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 1012 | |
Holger Hans Peter Freyther | 5274646 | 2012-03-01 20:30:32 +0100 | [diff] [blame] | 1013 | msgb_free(msg); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1014 | |
| 1015 | return rc; |
| 1016 | } |
| 1017 | |
| 1018 | /* 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] | 1019 | static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1020 | { |
| 1021 | int rc = 0; |
| 1022 | |
| 1023 | if (what & BSC_FD_READ) |
| 1024 | rc = handle_nsip_read(bfd); |
| 1025 | if (what & BSC_FD_WRITE) |
| 1026 | rc = handle_nsip_write(bfd); |
| 1027 | |
| 1028 | return rc; |
| 1029 | } |
| 1030 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1031 | /*! \brief Create a listening socket for GPRS NS/UDP/IP |
| 1032 | * \param[in] nsi NS protocol instance to listen |
| 1033 | * \returns >=0 (fd) in case of success, negative in case of error |
| 1034 | * |
| 1035 | * A call to this function will create a UDP socket bound to the port |
| 1036 | * number and IP address specified in the NS protocol instance. The |
| 1037 | * file descriptor of the socket will be stored in nsi->nsip.fd. |
| 1038 | */ |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 1039 | int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1040 | { |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 1041 | struct in_addr in; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1042 | int ret; |
| 1043 | |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 1044 | in.s_addr = htonl(nsi->nsip.local_ip); |
| 1045 | |
| 1046 | nsi->nsip.fd.cb = nsip_fd_cb; |
| 1047 | nsi->nsip.fd.data = nsi; |
| 1048 | ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM, |
| 1049 | IPPROTO_UDP, inet_ntoa(in), |
| 1050 | nsi->nsip.local_port, OSMO_SOCK_F_BIND); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1051 | if (ret < 0) |
| 1052 | return ret; |
| 1053 | |
Holger Hans Peter Freyther | 2c3393d | 2013-03-25 11:59:58 +0100 | [diff] [blame] | 1054 | ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS, |
| 1055 | &nsi->nsip.dscp, sizeof(nsi->nsip.dscp)); |
| 1056 | if (ret < 0) |
| 1057 | LOGP(DNS, LOGL_ERROR, |
| 1058 | "Failed to set the DSCP to %d with ret(%d) errno(%d)\n", |
| 1059 | nsi->nsip.dscp, ret, errno); |
| 1060 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1061 | |
| 1062 | return ret; |
| 1063 | } |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1064 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1065 | /*! \brief Initiate a RESET procedure |
| 1066 | * \param[in] nsvc NS-VC in which to start the procedure |
| 1067 | * \param[in] cause Numeric NS cause value |
| 1068 | * |
| 1069 | * This is a high-level function initiating a NS-RESET procedure. It |
| 1070 | * will not only send a NS-RESET, but also set the state to BLOCKED and |
| 1071 | * start the Tns-reset timer. |
| 1072 | */ |
Holger Hans Peter Freyther | 5617d99 | 2010-05-23 21:18:01 +0800 | [diff] [blame] | 1073 | void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1074 | { |
Harald Welte | b1020d5 | 2010-05-25 22:17:30 +0200 | [diff] [blame] | 1075 | LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n", |
| 1076 | nsvc->nsei); |
| 1077 | |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1078 | /* Mark NS-VC locally as blocked and dead */ |
| 1079 | nsvc->state = NSE_S_BLOCKED; |
| 1080 | /* Send NS-RESET PDU */ |
| 1081 | if (gprs_ns_tx_reset(nsvc, cause) < 0) { |
| 1082 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
| 1083 | nsvc->nsei); |
| 1084 | } |
| 1085 | /* Start Tns-reset */ |
| 1086 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1089 | /*! \brief Establish a NS connection (from the BSS) to the SGSN |
| 1090 | * \param nsi NS-instance |
| 1091 | * \param[in] dest Destination IP/Port |
| 1092 | * \param[in] nsei NSEI of the to-be-established NS-VC |
| 1093 | * \param[in] nsvci NSVCI of the to-be-established NS-VC |
| 1094 | * \returns struct gprs_nsvc representing the new NS-VC |
| 1095 | * |
| 1096 | * This function will establish a single NS/UDP/IP connection in uplink |
| 1097 | * (BSS to SGSN) direction. |
| 1098 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1099 | struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 1100 | struct sockaddr_in *dest, uint16_t nsei, |
| 1101 | uint16_t nsvci) |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1102 | { |
| 1103 | struct gprs_nsvc *nsvc; |
| 1104 | |
| 1105 | nsvc = nsvc_by_rem_addr(nsi, dest); |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 1106 | if (!nsvc) |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1107 | nsvc = gprs_nsvc_create(nsi, nsvci); |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 1108 | nsvc->ip.bts_addr = *dest; |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 1109 | nsvc->nsei = nsei; |
| 1110 | nsvc->nsvci = nsvci; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1111 | nsvc->remote_end_is_sgsn = 1; |
| 1112 | |
Holger Hans Peter Freyther | 5617d99 | 2010-05-23 21:18:01 +0800 | [diff] [blame] | 1113 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 1114 | return nsvc; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1115 | } |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1116 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 1117 | void gprs_ns_set_log_ss(int ss) |
| 1118 | { |
| 1119 | DNS = ss; |
| 1120 | } |
| 1121 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1122 | /*! }@ */ |