Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1 | /* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg); |
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 |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | /* Some introduction into NS: NS is used typically on top of frame relay, |
| 25 | * but in the ip.access world it is encapsulated in UDP packets. It serves |
| 26 | * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't |
| 27 | * do much, apart from providing congestion notification and status indication. |
| 28 | * |
| 29 | * Terms: |
| 30 | * NS Network Service |
| 31 | * NSVC NS Virtual Connection |
| 32 | * NSEI NS Entity Identifier |
| 33 | * NSVL NS Virtual Link |
| 34 | * NSVLI NS Virtual Link Identifier |
| 35 | * BVC BSSGP Virtual Connection |
| 36 | * BVCI BSSGP Virtual Connection Identifier |
| 37 | * NSVCG NS Virtual Connection Goup |
| 38 | * Blocked NS-VC cannot be used for user traffic |
| 39 | * Alive Ability of a NS-VC to provide communication |
| 40 | * |
| 41 | * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will |
| 42 | * therefore identify the BSSGP virtual connection by a BVCI passed down to NS. |
| 43 | * NS then has to firgure out which NSVC's are responsible for this BVCI. |
| 44 | * Those mappings are administratively configured. |
| 45 | */ |
| 46 | |
| 47 | #include <stdlib.h> |
| 48 | #include <unistd.h> |
| 49 | #include <errno.h> |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 50 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 51 | |
| 52 | #include <arpa/inet.h> |
| 53 | |
| 54 | #include <openbsc/gsm_data.h> |
| 55 | #include <osmocore/msgb.h> |
| 56 | #include <osmocore/tlv.h> |
| 57 | #include <osmocore/talloc.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 58 | #include <osmocore/select.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 59 | #include <openbsc/debug.h> |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 60 | #include <openbsc/signal.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 61 | #include <openbsc/gprs_ns.h> |
| 62 | #include <openbsc/gprs_bssgp.h> |
| 63 | |
| 64 | #define NS_ALLOC_SIZE 1024 |
| 65 | |
| 66 | static const struct tlv_definition ns_att_tlvdef = { |
| 67 | .def = { |
| 68 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 69 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 70 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 71 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 72 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 73 | }, |
| 74 | }; |
| 75 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 76 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 77 | static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 78 | uint16_t nsvci) |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 79 | { |
| 80 | struct gprs_nsvc *nsvc; |
| 81 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 82 | if (nsvc->nsvci == nsvci) |
| 83 | return nsvc; |
| 84 | } |
| 85 | return NULL; |
| 86 | } |
| 87 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 88 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 89 | static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 90 | uint16_t nsei) |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 91 | { |
| 92 | struct gprs_nsvc *nsvc; |
| 93 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 94 | if (nsvc->nsei == nsei) |
| 95 | return nsvc; |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 101 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 102 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 103 | struct sockaddr_in *sin) |
| 104 | { |
| 105 | struct gprs_nsvc *nsvc; |
| 106 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
Harald Welte | cad8301 | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 107 | if (nsvc->ip.bts_addr.sin_addr.s_addr == |
| 108 | sin->sin_addr.s_addr && |
| 109 | nsvc->ip.bts_addr.sin_port == sin->sin_port) |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 110 | return nsvc; |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 115 | static void gprs_ns_timer_cb(void *data); |
| 116 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 117 | static 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] | 118 | { |
| 119 | struct gprs_nsvc *nsvc; |
| 120 | |
| 121 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 122 | nsvc->nsvci = nsvci; |
| 123 | /* before RESET procedure: BLOCKED and DEAD */ |
| 124 | nsvc->state = NSE_S_BLOCKED; |
| 125 | nsvc->nsi = nsi; |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 126 | nsvc->timer.cb = gprs_ns_timer_cb; |
| 127 | nsvc->timer.data = nsvc; |
| 128 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 129 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 130 | |
| 131 | return nsvc; |
| 132 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 133 | |
Harald Welte | 1194b58 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 134 | static void nsvc_delete(struct gprs_nsvc *nsvc) |
| 135 | { |
| 136 | if (bsc_timer_pending(&nsvc->timer)) |
| 137 | bsc_del_timer(&nsvc->timer); |
| 138 | llist_del(&nsvc->list); |
| 139 | talloc_free(nsvc); |
| 140 | } |
| 141 | |
Harald Welte | 1389ac7 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 142 | static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal, |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 143 | uint8_t cause) |
| 144 | { |
| 145 | struct ns_signal_data nssd; |
| 146 | |
| 147 | nssd.nsvc = nsvc; |
| 148 | nssd.cause = cause; |
| 149 | |
| 150 | dispatch_signal(SS_NS, signal, &nssd); |
| 151 | } |
| 152 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 153 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 345223e | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 154 | static const struct value_string ns_cause_str[] = { |
| 155 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 156 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 157 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 158 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 159 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 160 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 161 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 162 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 163 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 345223e | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 164 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 165 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 166 | { 0, NULL } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 167 | }; |
| 168 | |
Harald Welte | 345223e | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 169 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 170 | { |
Harald Welte | 345223e | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 171 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 174 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 175 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 176 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 177 | { |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 178 | int ret; |
| 179 | |
| 180 | switch (nsvc->nsi->ll) { |
| 181 | case GPRS_NS_LL_UDP: |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 182 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 183 | break; |
| 184 | default: |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 185 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 186 | msgb_free(msg); |
| 187 | ret = -EIO; |
| 188 | break; |
| 189 | } |
| 190 | return ret; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 191 | } |
| 192 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 193 | 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] | 194 | { |
| 195 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 196 | struct gprs_ns_hdr *nsh; |
| 197 | |
| 198 | if (!msg) |
| 199 | return -ENOMEM; |
| 200 | |
| 201 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 202 | |
| 203 | nsh->pdu_type = pdu_type; |
| 204 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 205 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 208 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 209 | { |
| 210 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 211 | struct gprs_ns_hdr *nsh; |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 212 | uint16_t nsvci = htons(nsvc->nsvci); |
| 213 | uint16_t nsei = htons(nsvc->nsei); |
| 214 | |
| 215 | if (!msg) |
| 216 | return -ENOMEM; |
| 217 | |
Harald Welte | 34caeb3 | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 218 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n", |
| 219 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 220 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 221 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 222 | nsh->pdu_type = NS_PDUT_RESET; |
| 223 | |
| 224 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 225 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 226 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 227 | |
| 228 | return gprs_ns_tx(nsvc, msg); |
| 229 | |
| 230 | } |
| 231 | |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 232 | int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause, |
| 233 | uint16_t bvci, struct msgb *orig_msg) |
| 234 | { |
| 235 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 236 | struct gprs_ns_hdr *nsh; |
| 237 | uint16_t nsvci = htons(nsvc->nsvci); |
| 238 | |
| 239 | bvci = htons(bvci); |
| 240 | |
| 241 | if (!msg) |
| 242 | return -ENOMEM; |
| 243 | |
Harald Welte | 34caeb3 | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 244 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n", |
| 245 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 246 | |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 247 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 248 | nsh->pdu_type = NS_PDUT_STATUS; |
| 249 | |
| 250 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 251 | |
| 252 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 253 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 254 | cause == NS_CAUSE_NSVC_UNKNOWN) |
| 255 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 256 | |
| 257 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 258 | switch (cause) { |
| 259 | case NS_CAUSE_SEM_INCORR_PDU: |
| 260 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 261 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 262 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 263 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 264 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 265 | orig_msg->l2h); |
| 266 | break; |
| 267 | default: |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | /* Section 9.2.7.3: Static conditions for BVCI */ |
| 272 | if (cause == NS_CAUSE_BVCI_UNKNOWN) |
| 273 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci); |
| 274 | |
| 275 | return gprs_ns_tx(nsvc, msg); |
| 276 | } |
| 277 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 278 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause) |
| 279 | { |
| 280 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 281 | struct gprs_ns_hdr *nsh; |
| 282 | uint16_t nsvci = htons(nsvc->nsvci); |
| 283 | |
| 284 | if (!msg) |
| 285 | return -ENOMEM; |
| 286 | |
Harald Welte | 34caeb3 | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 287 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n", |
| 288 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 289 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 290 | /* be conservative and mark it as blocked even now! */ |
| 291 | nsvc->state |= NSE_S_BLOCKED; |
| 292 | |
| 293 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 294 | nsh->pdu_type = NS_PDUT_BLOCK; |
| 295 | |
| 296 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 297 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 298 | |
| 299 | return gprs_ns_tx(nsvc, msg); |
| 300 | } |
| 301 | |
| 302 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc) |
| 303 | { |
Harald Welte | 34caeb3 | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 304 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n", |
| 305 | nsvc->nsei, nsvc->nsvci); |
| 306 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 307 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 308 | } |
| 309 | |
Harald Welte | bca900d | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 310 | int gprs_ns_tx_alive(struct gprs_nsvc *nsvc) |
| 311 | { |
| 312 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n", |
| 313 | nsvc->nsei, nsvc->nsvci); |
| 314 | |
| 315 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 316 | } |
| 317 | |
| 318 | int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc) |
| 319 | { |
| 320 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n", |
| 321 | nsvc->nsei, nsvc->nsvci); |
| 322 | |
| 323 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
| 324 | } |
| 325 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 326 | #define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */ |
| 327 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 328 | static const uint8_t timer_mode_tout[_NSVC_TIMER_NR] = { |
| 329 | [NSVC_TIMER_TNS_RESET] = 60, |
| 330 | [NSVC_TIMER_TNS_ALIVE] = 3, |
| 331 | [NSVC_TIMER_TNS_TEST] = 30, |
| 332 | }; |
| 333 | |
Harald Welte | 811c497 | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 334 | static const struct value_string timer_mode_strs[] = { |
| 335 | { NSVC_TIMER_TNS_RESET, "tns-reset" }, |
| 336 | { NSVC_TIMER_TNS_ALIVE, "tns-alive" }, |
| 337 | { NSVC_TIMER_TNS_TEST, "tns-test" }, |
| 338 | { 0, NULL } |
| 339 | }; |
| 340 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 341 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 342 | { |
Harald Welte | d4eaf80 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 343 | DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n", |
| 344 | nsvc->nsei, get_value_string(timer_mode_strs, mode), |
Harald Welte | 811c497 | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 345 | timer_mode_tout[mode]); |
| 346 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 347 | if (bsc_timer_pending(&nsvc->timer)) |
| 348 | bsc_del_timer(&nsvc->timer); |
| 349 | |
| 350 | nsvc->timer_mode = mode; |
| 351 | bsc_schedule_timer(&nsvc->timer, timer_mode_tout[mode], 0); |
| 352 | } |
| 353 | |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 354 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 355 | { |
| 356 | struct gprs_nsvc *nsvc = data; |
| 357 | |
Harald Welte | d4eaf80 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 358 | DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n", |
| 359 | nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode), |
Harald Welte | 811c497 | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 360 | timer_mode_tout[nsvc->timer_mode]); |
| 361 | |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 362 | switch (nsvc->timer_mode) { |
| 363 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 364 | /* Tns-alive case: we expired without response ! */ |
| 365 | nsvc->alive_retries++; |
| 366 | if (nsvc->alive_retries > NS_ALIVE_RETRIES) { |
| 367 | /* mark as dead and blocked */ |
| 368 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 369 | LOGP(DNS, LOGL_NOTICE, |
| 370 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 371 | "%u times, blocking NS-VC\n", nsvc->nsei, |
| 372 | NS_ALIVE_RETRIES); |
Harald Welte | b778d2c | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 373 | ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0); |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 374 | ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 375 | return; |
| 376 | } |
Harald Welte | bca900d | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 377 | /* Tns-test case: send NS-ALIVE PDU */ |
| 378 | gprs_ns_tx_alive(nsvc); |
| 379 | /* start Tns-alive timer */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 380 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 381 | break; |
| 382 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 383 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | bca900d | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 384 | gprs_ns_tx_alive(nsvc); |
| 385 | /* start Tns-alive timer (transition into faster |
| 386 | * alive retransmissions) */ |
Harald Welte | 811c497 | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 387 | nsvc->alive_retries = 0; |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 388 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 389 | break; |
| 390 | case NSVC_TIMER_TNS_RESET: |
| 391 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 90de93e | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 392 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 393 | /* Re-start Tns-reset timer */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 394 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 395 | break; |
Harald Welte | 1389ac7 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 396 | case _NSVC_TIMER_NR: |
| 397 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 398 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* Section 9.2.6 */ |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 402 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 403 | { |
| 404 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 405 | struct gprs_ns_hdr *nsh; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 406 | uint16_t nsvci, nsei; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 407 | |
| 408 | if (!msg) |
| 409 | return -ENOMEM; |
| 410 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 411 | nsvci = htons(nsvc->nsvci); |
| 412 | nsei = htons(nsvc->nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 413 | |
| 414 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 415 | |
| 416 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 417 | |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 418 | 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] | 419 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 420 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 421 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 422 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 423 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 424 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 425 | } |
| 426 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 427 | /* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */ |
| 428 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 429 | { |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 430 | struct gprs_nsvc *nsvc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 431 | struct gprs_ns_hdr *nsh; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 432 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 433 | |
| 434 | nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg)); |
| 435 | if (!nsvc) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 436 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 437 | "to NS-VC!\n", msgb_nsei(msg)); |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 438 | return -EINVAL; |
| 439 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 440 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 441 | if (!(nsvc->state & NSE_S_ALIVE)) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 442 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n", |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 443 | nsvc->nsei); |
| 444 | return -EBUSY; |
| 445 | } |
| 446 | if (nsvc->state & NSE_S_BLOCKED) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 447 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n", |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 448 | nsvc->nsei); |
| 449 | return -EBUSY; |
| 450 | } |
| 451 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 452 | nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3); |
| 453 | if (!nsh) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 454 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 455 | return -EIO; |
| 456 | } |
| 457 | |
| 458 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 459 | /* spare octet in data[0] */ |
| 460 | nsh->data[1] = bvci >> 8; |
| 461 | nsh->data[2] = bvci & 0xff; |
| 462 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 463 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* Section 9.2.10: receive side */ |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 467 | 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] | 468 | { |
| 469 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 470 | uint16_t bvci; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 471 | |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 472 | if (nsvc->state & NSE_S_BLOCKED) |
| 473 | return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED, |
| 474 | 0, msg); |
| 475 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 476 | /* spare octet in data[0] */ |
| 477 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | fd3fa1d | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 478 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 479 | msgb_bvci(msg) = bvci; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 480 | |
| 481 | /* call upper layer (BSSGP) */ |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 482 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* Section 9.2.7 */ |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 486 | 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] | 487 | { |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 488 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 489 | struct tlv_parsed tp; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 490 | uint8_t cause; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 491 | int rc; |
| 492 | |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 493 | LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 494 | |
| 495 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 496 | |
| 497 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 498 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 499 | return -EINVAL; |
| 500 | } |
| 501 | |
| 502 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 503 | LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | /* Section 7.3 */ |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 509 | 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] | 510 | { |
| 511 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 512 | struct tlv_parsed tp; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 513 | uint8_t *cause; |
| 514 | uint16_t *nsvci, *nsei; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 515 | int rc; |
| 516 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 517 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 518 | |
| 519 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 520 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 521 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 522 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 523 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 524 | return -EINVAL; |
| 525 | } |
| 526 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 527 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 528 | nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 529 | nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 530 | |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 531 | 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] | 532 | nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause)); |
| 533 | |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 534 | /* Mark NS-VC as blocked and alive */ |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 535 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | d7c02ad | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 536 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 537 | nsvc->nsei = ntohs(*nsei); |
| 538 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 539 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 540 | /* start the test procedure */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 541 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 542 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 543 | /* inform interested parties about the fact that this NSVC |
| 544 | * has received RESET */ |
Harald Welte | 1389ac7 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 545 | ns_dispatch_signal(nsvc, S_NS_RESET, *cause); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 546 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 547 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 548 | } |
| 549 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 550 | static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 551 | { |
| 552 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 553 | struct tlv_parsed tp; |
| 554 | uint8_t *cause; |
| 555 | int rc; |
| 556 | |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 557 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 558 | |
| 559 | nsvc->state |= NSE_S_BLOCKED; |
| 560 | |
| 561 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 562 | |
| 563 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 564 | !TLVP_PRESENT(&tp, NS_IE_VCI)) { |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 565 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 566 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 567 | return -EINVAL; |
| 568 | } |
| 569 | |
| 570 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 571 | //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 572 | |
Harald Welte | 1389ac7 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 573 | ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 574 | |
| 575 | return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK); |
| 576 | } |
| 577 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 578 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 579 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 580 | struct sockaddr_in *saddr) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 581 | { |
| 582 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 583 | struct gprs_nsvc *nsvc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 584 | int rc = 0; |
| 585 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 586 | /* look up the NSVC based on source address */ |
| 587 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 588 | if (!nsvc) { |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 589 | struct tlv_parsed tp; |
| 590 | uint16_t nsei; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 591 | /* Only the RESET procedure creates a new NSVC */ |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 592 | if (nsh->pdu_type != NS_PDUT_RESET) { |
Harald Welte | 8c2440e | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 593 | struct gprs_nsvc fake_nsvc; |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 594 | LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x " |
Harald Welte | ef1226e | 2010-05-11 18:38:36 +0200 | [diff] [blame] | 595 | "from %s:%u for non-existing NS-VC\n", |
| 596 | nsh->pdu_type, inet_ntoa(saddr->sin_addr), |
| 597 | ntohs(saddr->sin_port)); |
Harald Welte | 8c2440e | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 598 | /* Since we have no NSVC, we have to create a fake */ |
| 599 | fake_nsvc.nsvci = fake_nsvc.nsei = 0; |
| 600 | fake_nsvc.nsi = nsi; |
| 601 | fake_nsvc.ip.bts_addr = *saddr; |
| 602 | fake_nsvc.state = NSE_S_ALIVE; |
Harald Welte | 8c2440e | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 603 | return gprs_ns_tx_status(&fake_nsvc, |
| 604 | NS_CAUSE_PDU_INCOMP_PSTATE, 0, |
| 605 | msg); |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 606 | } |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 607 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 608 | msgb_l2len(msg), 0, 0); |
| 609 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 610 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 611 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 612 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 613 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
| 614 | msg); |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 615 | return -EINVAL; |
| 616 | } |
| 617 | nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI)); |
| 618 | /* Check if we already know this NSEI, the remote end might |
| 619 | * simply have changed addresses, or it is a SGSN */ |
| 620 | nsvc = nsvc_by_nsei(nsi, nsei); |
| 621 | if (!nsvc) { |
| 622 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n", |
| 623 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
| 624 | nsvc = nsvc_create(nsi, 0xffff); |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 625 | } |
Harald Welte | 7c209eb | 2010-05-11 18:40:45 +0200 | [diff] [blame] | 626 | /* Update the remote peer IP address/port */ |
| 627 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 628 | } else |
| 629 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 630 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 631 | switch (nsh->pdu_type) { |
| 632 | case NS_PDUT_ALIVE: |
Harald Welte | 1194b58 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 633 | /* If we're dead and blocked and suddenly receive a |
| 634 | * NS-ALIVE out of the blue, we might have been re-started |
| 635 | * and should send a NS-RESET to make sure everything recovers |
| 636 | * fine. */ |
| 637 | if (nsvc->state == NSE_S_BLOCKED) |
| 638 | rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE); |
| 639 | else |
| 640 | rc = gprs_ns_tx_alive_ack(nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 641 | break; |
| 642 | case NS_PDUT_ALIVE_ACK: |
| 643 | /* stop Tns-alive */ |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 644 | bsc_del_timer(&nsvc->timer); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 645 | /* start Tns-test */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 646 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 24b3131 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 647 | if (nsvc->remote_end_is_sgsn) { |
| 648 | /* FIXME: this should be one level higher */ |
| 649 | if (nsvc->state & NSE_S_BLOCKED) |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 650 | rc = gprs_ns_tx_unblock(nsvc); |
Harald Welte | 24b3131 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 651 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 652 | break; |
| 653 | case NS_PDUT_UNITDATA: |
| 654 | /* actual user data */ |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 655 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 656 | break; |
| 657 | case NS_PDUT_STATUS: |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 658 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 659 | break; |
| 660 | case NS_PDUT_RESET: |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 661 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 662 | break; |
| 663 | case NS_PDUT_RESET_ACK: |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 664 | 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] | 665 | /* mark NS-VC as blocked + active */ |
| 666 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 667 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 668 | if (nsvc->remote_end_is_sgsn) { |
| 669 | /* stop RESET timer */ |
| 670 | bsc_del_timer(&nsvc->timer); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 671 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
Harald Welte | 90de93e | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 672 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 673 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 674 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 675 | break; |
| 676 | case NS_PDUT_UNBLOCK: |
| 677 | /* Section 7.2: unblocking procedure */ |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 678 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 679 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 680 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 681 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 682 | break; |
| 683 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 684 | 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] | 685 | /* mark NS-VC as unblocked + active */ |
| 686 | nsvc->state = NSE_S_ALIVE; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 687 | nsvc->remote_state = NSE_S_ALIVE; |
Harald Welte | f6d67c0 | 2010-05-12 14:18:46 +0000 | [diff] [blame] | 688 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 689 | break; |
| 690 | case NS_PDUT_BLOCK: |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 691 | rc = gprs_ns_rx_block(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 692 | break; |
| 693 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 694 | 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] | 695 | /* mark remote NS-VC as blocked + active */ |
| 696 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 697 | break; |
| 698 | default: |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 699 | 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] | 700 | nsvc->nsei, nsh->pdu_type); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 701 | rc = -EINVAL; |
| 702 | break; |
| 703 | } |
| 704 | return rc; |
| 705 | } |
| 706 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 707 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb) |
| 708 | { |
| 709 | struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst); |
| 710 | |
| 711 | nsi->cb = cb; |
| 712 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
| 713 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 714 | return nsi; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 718 | { |
| 719 | /* FIXME: clear all timers */ |
| 720 | |
| 721 | /* recursively free the NSI and all its NSVCs */ |
| 722 | talloc_free(nsi); |
| 723 | } |
| 724 | |
| 725 | |
| 726 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 727 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 728 | |
| 729 | /* Read a single NS-over-IP message */ |
| 730 | static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error, |
| 731 | struct sockaddr_in *saddr) |
| 732 | { |
| 733 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS"); |
| 734 | int ret = 0; |
| 735 | socklen_t saddr_len = sizeof(*saddr); |
| 736 | |
| 737 | if (!msg) { |
| 738 | *error = -ENOMEM; |
| 739 | return NULL; |
| 740 | } |
| 741 | |
| 742 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 743 | (struct sockaddr *)saddr, &saddr_len); |
| 744 | if (ret < 0) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 745 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 746 | strerror(errno)); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 747 | msgb_free(msg); |
| 748 | *error = ret; |
| 749 | return NULL; |
| 750 | } else if (ret == 0) { |
| 751 | msgb_free(msg); |
| 752 | *error = ret; |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | msg->l2h = msg->data; |
| 757 | msgb_put(msg, ret); |
| 758 | |
| 759 | return msg; |
| 760 | } |
| 761 | |
| 762 | static int handle_nsip_read(struct bsc_fd *bfd) |
| 763 | { |
| 764 | int error; |
| 765 | struct sockaddr_in saddr; |
| 766 | struct gprs_ns_inst *nsi = bfd->data; |
| 767 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 768 | |
| 769 | if (!msg) |
| 770 | return error; |
| 771 | |
| 772 | return gprs_ns_rcvmsg(nsi, msg, &saddr); |
| 773 | } |
| 774 | |
| 775 | static int handle_nsip_write(struct bsc_fd *bfd) |
| 776 | { |
| 777 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 778 | return -EIO; |
| 779 | } |
| 780 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 781 | int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 782 | { |
| 783 | int rc; |
| 784 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 785 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 786 | |
| 787 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 788 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 789 | |
| 790 | talloc_free(msg); |
| 791 | |
| 792 | return rc; |
| 793 | } |
| 794 | |
| 795 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
| 796 | static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 797 | { |
| 798 | int rc = 0; |
| 799 | |
| 800 | if (what & BSC_FD_READ) |
| 801 | rc = handle_nsip_read(bfd); |
| 802 | if (what & BSC_FD_WRITE) |
| 803 | rc = handle_nsip_write(bfd); |
| 804 | |
| 805 | return rc; |
| 806 | } |
| 807 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 808 | extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port, |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 809 | int (*cb)(struct bsc_fd *fd, unsigned int what)); |
| 810 | |
| 811 | /* Listen for incoming GPRS packets */ |
| 812 | int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port) |
| 813 | { |
| 814 | int ret; |
| 815 | |
| 816 | ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb); |
| 817 | if (ret < 0) |
| 818 | return ret; |
| 819 | |
| 820 | nsi->ll = GPRS_NS_LL_UDP; |
| 821 | nsi->nsip.fd.data = nsi; |
| 822 | |
| 823 | return ret; |
| 824 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 825 | |
| 826 | /* Establish a connection (from the BSS) to the SGSN */ |
| 827 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 828 | struct sockaddr_in *dest, uint16_t nsei, |
| 829 | uint16_t nsvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 830 | { |
| 831 | struct gprs_nsvc *nsvc; |
| 832 | |
| 833 | nsvc = nsvc_by_rem_addr(nsi, dest); |
Harald Welte | cad8301 | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 834 | if (!nsvc) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 835 | nsvc = nsvc_create(nsi, nsvci); |
Harald Welte | cad8301 | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 836 | nsvc->ip.bts_addr = *dest; |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 837 | nsvc->nsei = nsei; |
| 838 | nsvc->nsvci = nsvci; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 839 | nsvc->remote_end_is_sgsn = 1; |
| 840 | |
| 841 | /* Initiate a RESET procedure */ |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 842 | /* Mark NS-VC locally as blocked and dead */ |
| 843 | nsvc->state = NSE_S_BLOCKED; |
| 844 | /* Send NS-RESET PDU */ |
Harald Welte | 90de93e | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 845 | if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 846 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 847 | nsei); |
| 848 | } |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 849 | /* Start Tns-reset */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 850 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 851 | |
| 852 | return nsvc; |
| 853 | } |
Harald Welte | 1194b58 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 854 | |
| 855 | #include <vty/vty.h> |
| 856 | #include <vty/command.h> |
| 857 | |
| 858 | static struct gprs_ns_inst *vty_nsi = NULL; |
| 859 | |
| 860 | static struct cmd_node ns_node = { |
| 861 | NS_NODE, |
| 862 | "%s(ns)#", |
| 863 | 1, |
| 864 | }; |
| 865 | |
| 866 | static int config_write_ns(struct vty *vty) |
| 867 | { |
| 868 | struct gprs_nsvc *nsvc; |
| 869 | |
| 870 | vty_out(vty, "ns%s", VTY_NEWLINE); |
| 871 | |
| 872 | llist_for_each_entry(nsvc, &vty_nsi->gprs_nsvcs, list) { |
| 873 | if (!nsvc->persistent) |
| 874 | continue; |
| 875 | vty_out(vty, " nse %u nsvci %u%s", |
| 876 | nsvc->nsei, nsvc->nsvci, VTY_NEWLINE); |
| 877 | vty_out(vty, " nse %u remote-role %s%s", |
| 878 | nsvc->nsei, nsvc->remote_end_is_sgsn ? "sgsn" : "bss", |
| 879 | VTY_NEWLINE); |
| 880 | if (nsvc->nsi->ll == GPRS_NS_LL_UDP) { |
| 881 | vty_out(vty, " nse %u remote-ip %s%s", |
| 882 | nsvc->nsei, |
| 883 | inet_ntoa(nsvc->ip.bts_addr.sin_addr), |
| 884 | VTY_NEWLINE); |
| 885 | vty_out(vty, " nse %u remote-port %u%s", |
| 886 | nsvc->nsei, ntohs(nsvc->ip.bts_addr.sin_port), |
| 887 | VTY_NEWLINE); |
| 888 | } |
| 889 | vty_out(vty, "%s", VTY_NEWLINE); |
| 890 | } |
| 891 | |
| 892 | return CMD_SUCCESS; |
| 893 | } |
| 894 | |
| 895 | DEFUN(cfg_ns, cfg_ns_cmd, |
| 896 | "ns", |
| 897 | "Configure the GPRS Network Service") |
| 898 | { |
| 899 | vty->node = NS_NODE; |
| 900 | return CMD_SUCCESS; |
| 901 | } |
| 902 | |
| 903 | DEFUN(show_ns, show_ns_cmd, "show ns", |
| 904 | SHOW_STR "Display information about the NS protocol") |
| 905 | { |
| 906 | struct gprs_ns_inst *nsi = vty_nsi; |
| 907 | struct gprs_nsvc *nsvc; |
| 908 | |
| 909 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 910 | vty_out(vty, "NSEI %5u, NS-VC %5u, Remote: %-4s, %5s %9s", |
| 911 | nsvc->nsei, nsvc->nsvci, |
| 912 | nsvc->remote_end_is_sgsn ? "SGSN" : "BSS", |
| 913 | nsvc->state & NSE_S_ALIVE ? "ALIVE" : "DEAD", |
| 914 | nsvc->state & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED"); |
| 915 | if (nsvc->nsi->ll == GPRS_NS_LL_UDP) |
| 916 | vty_out(vty, ", %15s:%u", |
| 917 | inet_ntoa(nsvc->ip.bts_addr.sin_addr), |
| 918 | ntohs(nsvc->ip.bts_addr.sin_port)); |
| 919 | vty_out(vty, "%s", VTY_NEWLINE); |
| 920 | } |
| 921 | |
| 922 | return CMD_SUCCESS; |
| 923 | } |
| 924 | |
| 925 | |
| 926 | #define NSE_CMD_STR "NS Entity\n" "NS Entity ID (NSEI)\n" |
| 927 | |
| 928 | DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd, |
| 929 | "nse <0-65535> nsvci <0-65534>", |
| 930 | NSE_CMD_STR |
| 931 | "NS Virtual Connection\n" |
| 932 | "NS Virtual Connection ID (NSVCI)\n" |
| 933 | ) |
| 934 | { |
| 935 | uint16_t nsei = atoi(argv[0]); |
| 936 | uint16_t nsvci = atoi(argv[1]); |
| 937 | struct gprs_nsvc *nsvc; |
| 938 | |
| 939 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 940 | if (!nsvc) { |
| 941 | nsvc = nsvc_create(vty_nsi, nsvci); |
| 942 | nsvc->nsei = nsei; |
| 943 | } |
| 944 | nsvc->nsvci = nsvci; |
| 945 | /* All NSVCs that are explicitly configured by VTY are |
| 946 | * marked as persistent so we can write them to the config |
| 947 | * file at some later point */ |
| 948 | nsvc->persistent = 1; |
| 949 | |
| 950 | return CMD_SUCCESS; |
| 951 | } |
| 952 | |
| 953 | DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd, |
| 954 | "nse <0-65535> remote-ip A.B.C.D", |
| 955 | NSE_CMD_STR |
| 956 | "Remote IP Address\n" |
| 957 | "Remote IP Address\n") |
| 958 | { |
| 959 | uint16_t nsei = atoi(argv[0]); |
| 960 | struct gprs_nsvc *nsvc; |
| 961 | |
| 962 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 963 | if (!nsvc) { |
| 964 | vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE); |
| 965 | return CMD_WARNING; |
| 966 | } |
| 967 | inet_aton(argv[1], &nsvc->ip.bts_addr.sin_addr); |
| 968 | |
| 969 | return CMD_SUCCESS; |
| 970 | |
| 971 | } |
| 972 | |
| 973 | DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd, |
| 974 | "nse <0-65535> remote-port <0-65535>", |
| 975 | NSE_CMD_STR |
| 976 | "Remote UDP Port\n" |
| 977 | "Remote UDP Port Number\n") |
| 978 | { |
| 979 | uint16_t nsei = atoi(argv[0]); |
| 980 | uint16_t port = atoi(argv[1]); |
| 981 | struct gprs_nsvc *nsvc; |
| 982 | |
| 983 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 984 | if (!nsvc) { |
| 985 | vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE); |
| 986 | return CMD_WARNING; |
| 987 | } |
| 988 | |
| 989 | nsvc->ip.bts_addr.sin_port = htons(port); |
| 990 | |
| 991 | return CMD_SUCCESS; |
| 992 | } |
| 993 | |
| 994 | DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd, |
| 995 | "nse <0-65535> remote-role (sgsn|bss)", |
| 996 | NSE_CMD_STR |
| 997 | "Remote NSE Role\n" |
| 998 | "Remote Peer is SGSN\n" |
| 999 | "Remote Peer is BSS\n") |
| 1000 | { |
| 1001 | uint16_t nsei = atoi(argv[0]); |
| 1002 | struct gprs_nsvc *nsvc; |
| 1003 | |
| 1004 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 1005 | if (!nsvc) { |
| 1006 | vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE); |
| 1007 | return CMD_WARNING; |
| 1008 | } |
| 1009 | |
| 1010 | if (!strcmp(argv[1], "sgsn")) |
| 1011 | nsvc->remote_end_is_sgsn = 1; |
| 1012 | else |
| 1013 | nsvc->remote_end_is_sgsn = 0; |
| 1014 | |
| 1015 | return CMD_SUCCESS; |
| 1016 | } |
| 1017 | |
| 1018 | DEFUN(cfg_no_nse, cfg_no_nse_cmd, |
| 1019 | "no nse <0-65535>", |
| 1020 | "Delete NS Entity\n" |
| 1021 | "Delete " NSE_CMD_STR) |
| 1022 | { |
| 1023 | uint16_t nsei = atoi(argv[0]); |
| 1024 | struct gprs_nsvc *nsvc; |
| 1025 | |
| 1026 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 1027 | if (!nsvc) { |
| 1028 | vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE); |
| 1029 | return CMD_WARNING; |
| 1030 | } |
| 1031 | |
| 1032 | nsvc_delete(nsvc); |
| 1033 | |
| 1034 | return CMD_SUCCESS; |
| 1035 | } |
| 1036 | |
| 1037 | int gprs_ns_vty_init(struct gprs_ns_inst *nsi) |
| 1038 | { |
| 1039 | vty_nsi = nsi; |
| 1040 | |
| 1041 | install_element(VIEW_NODE, &show_ns_cmd); |
| 1042 | |
| 1043 | install_element(CONFIG_NODE, &cfg_ns_cmd); |
| 1044 | install_node(&ns_node, config_write_ns); |
| 1045 | install_default(NS_NODE); |
| 1046 | install_element(NS_NODE, &cfg_nse_nsvci_cmd); |
| 1047 | install_element(NS_NODE, &cfg_nse_remoteip_cmd); |
| 1048 | install_element(NS_NODE, &cfg_nse_remoteport_cmd); |
| 1049 | install_element(NS_NODE, &cfg_nse_remoterole_cmd); |
| 1050 | install_element(NS_NODE, &cfg_no_nse_cmd); |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |