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