Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1 | /* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg); |
Harald Welte | 75bb820 | 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 | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 4 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 75bb820 | 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 50 | #include <stdint.h> |
Harald Welte | 75bb820 | 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 | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 58 | #include <osmocore/select.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 59 | #include <openbsc/debug.h> |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 60 | #include <openbsc/signal.h> |
Harald Welte | 75bb820 | 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 | f67a5f9 | 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 78 | uint16_t nsvci) |
Harald Welte | f67a5f9 | 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 | cd788ba | 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 90 | uint16_t nsei) |
Harald Welte | cd788ba | 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 | f67a5f9 | 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 | 517b77f | 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 | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 110 | return nsvc; |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 115 | static void gprs_ns_timer_cb(void *data); |
| 116 | |
Harald Welte | d85d9a9 | 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 | f67a5f9 | 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 | ec4c159 | 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 | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 129 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 130 | |
| 131 | return nsvc; |
| 132 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 133 | |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 134 | static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal, |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 135 | uint8_t cause) |
| 136 | { |
| 137 | struct ns_signal_data nssd; |
| 138 | |
| 139 | nssd.nsvc = nsvc; |
| 140 | nssd.cause = cause; |
| 141 | |
| 142 | dispatch_signal(SS_NS, signal, &nssd); |
| 143 | } |
| 144 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 145 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 146 | static const struct value_string ns_cause_str[] = { |
| 147 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 148 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 149 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 150 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 151 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 152 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 153 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 154 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 155 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 156 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 157 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 158 | { 0, NULL } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 159 | }; |
| 160 | |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 161 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 162 | { |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 163 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 164 | } |
| 165 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 166 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 167 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 168 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 169 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 170 | int ret; |
| 171 | |
| 172 | switch (nsvc->nsi->ll) { |
| 173 | case GPRS_NS_LL_UDP: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 174 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 175 | break; |
| 176 | default: |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 177 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 178 | msgb_free(msg); |
| 179 | ret = -EIO; |
| 180 | break; |
| 181 | } |
| 182 | return ret; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 183 | } |
| 184 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 185 | static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 186 | { |
| 187 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 188 | struct gprs_ns_hdr *nsh; |
| 189 | |
| 190 | if (!msg) |
| 191 | return -ENOMEM; |
| 192 | |
| 193 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 194 | |
| 195 | nsh->pdu_type = pdu_type; |
| 196 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 197 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 200 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 201 | { |
| 202 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 203 | struct gprs_ns_hdr *nsh; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 204 | uint16_t nsvci = htons(nsvc->nsvci); |
| 205 | uint16_t nsei = htons(nsvc->nsei); |
| 206 | |
| 207 | if (!msg) |
| 208 | return -ENOMEM; |
| 209 | |
Harald Welte | e21988c | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 210 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n", |
| 211 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 212 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 213 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 214 | nsh->pdu_type = NS_PDUT_RESET; |
| 215 | |
| 216 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 217 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 218 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 219 | |
| 220 | return gprs_ns_tx(nsvc, msg); |
| 221 | |
| 222 | } |
| 223 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 224 | int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause, |
| 225 | uint16_t bvci, struct msgb *orig_msg) |
| 226 | { |
| 227 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 228 | struct gprs_ns_hdr *nsh; |
| 229 | uint16_t nsvci = htons(nsvc->nsvci); |
| 230 | |
| 231 | bvci = htons(bvci); |
| 232 | |
| 233 | if (!msg) |
| 234 | return -ENOMEM; |
| 235 | |
Harald Welte | e21988c | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 236 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n", |
| 237 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 238 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 239 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 240 | nsh->pdu_type = NS_PDUT_STATUS; |
| 241 | |
| 242 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 243 | |
| 244 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 245 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 246 | cause == NS_CAUSE_NSVC_UNKNOWN) |
| 247 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 248 | |
| 249 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 250 | switch (cause) { |
| 251 | case NS_CAUSE_SEM_INCORR_PDU: |
| 252 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 253 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 254 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 255 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 256 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 257 | orig_msg->l2h); |
| 258 | break; |
| 259 | default: |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | /* Section 9.2.7.3: Static conditions for BVCI */ |
| 264 | if (cause == NS_CAUSE_BVCI_UNKNOWN) |
| 265 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci); |
| 266 | |
| 267 | return gprs_ns_tx(nsvc, msg); |
| 268 | } |
| 269 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 270 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause) |
| 271 | { |
| 272 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 273 | struct gprs_ns_hdr *nsh; |
| 274 | uint16_t nsvci = htons(nsvc->nsvci); |
| 275 | |
| 276 | if (!msg) |
| 277 | return -ENOMEM; |
| 278 | |
Harald Welte | e21988c | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 279 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n", |
| 280 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 281 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 282 | /* be conservative and mark it as blocked even now! */ |
| 283 | nsvc->state |= NSE_S_BLOCKED; |
| 284 | |
| 285 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 286 | nsh->pdu_type = NS_PDUT_BLOCK; |
| 287 | |
| 288 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 289 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 290 | |
| 291 | return gprs_ns_tx(nsvc, msg); |
| 292 | } |
| 293 | |
| 294 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc) |
| 295 | { |
Harald Welte | e21988c | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 296 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n", |
| 297 | nsvc->nsei, nsvc->nsvci); |
| 298 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 299 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 300 | } |
| 301 | |
Harald Welte | 9a4847f | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 302 | int gprs_ns_tx_alive(struct gprs_nsvc *nsvc) |
| 303 | { |
| 304 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n", |
| 305 | nsvc->nsei, nsvc->nsvci); |
| 306 | |
| 307 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 308 | } |
| 309 | |
| 310 | int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc) |
| 311 | { |
| 312 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n", |
| 313 | nsvc->nsei, nsvc->nsvci); |
| 314 | |
| 315 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
| 316 | } |
| 317 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 318 | #define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */ |
| 319 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 320 | static const uint8_t timer_mode_tout[_NSVC_TIMER_NR] = { |
| 321 | [NSVC_TIMER_TNS_RESET] = 60, |
| 322 | [NSVC_TIMER_TNS_ALIVE] = 3, |
| 323 | [NSVC_TIMER_TNS_TEST] = 30, |
| 324 | }; |
| 325 | |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 326 | static const struct value_string timer_mode_strs[] = { |
| 327 | { NSVC_TIMER_TNS_RESET, "tns-reset" }, |
| 328 | { NSVC_TIMER_TNS_ALIVE, "tns-alive" }, |
| 329 | { NSVC_TIMER_TNS_TEST, "tns-test" }, |
| 330 | { 0, NULL } |
| 331 | }; |
| 332 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 333 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 334 | { |
Harald Welte | 4574420 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 335 | DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n", |
| 336 | nsvc->nsei, get_value_string(timer_mode_strs, mode), |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 337 | timer_mode_tout[mode]); |
| 338 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 339 | if (bsc_timer_pending(&nsvc->timer)) |
| 340 | bsc_del_timer(&nsvc->timer); |
| 341 | |
| 342 | nsvc->timer_mode = mode; |
| 343 | bsc_schedule_timer(&nsvc->timer, timer_mode_tout[mode], 0); |
| 344 | } |
| 345 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 346 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 347 | { |
| 348 | struct gprs_nsvc *nsvc = data; |
| 349 | |
Harald Welte | 4574420 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 350 | DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n", |
| 351 | nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode), |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 352 | timer_mode_tout[nsvc->timer_mode]); |
| 353 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 354 | switch (nsvc->timer_mode) { |
| 355 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 356 | /* Tns-alive case: we expired without response ! */ |
| 357 | nsvc->alive_retries++; |
| 358 | if (nsvc->alive_retries > NS_ALIVE_RETRIES) { |
| 359 | /* mark as dead and blocked */ |
| 360 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 361 | LOGP(DNS, LOGL_NOTICE, |
| 362 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 363 | "%u times, blocking NS-VC\n", nsvc->nsei, |
| 364 | NS_ALIVE_RETRIES); |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 365 | ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0); |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 366 | ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 367 | return; |
| 368 | } |
Harald Welte | 9a4847f | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 369 | /* Tns-test case: send NS-ALIVE PDU */ |
| 370 | gprs_ns_tx_alive(nsvc); |
| 371 | /* start Tns-alive timer */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 372 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 373 | break; |
| 374 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 375 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | 9a4847f | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 376 | gprs_ns_tx_alive(nsvc); |
| 377 | /* start Tns-alive timer (transition into faster |
| 378 | * alive retransmissions) */ |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 379 | nsvc->alive_retries = 0; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 380 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 381 | break; |
| 382 | case NSVC_TIMER_TNS_RESET: |
| 383 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 384 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 385 | /* Re-start Tns-reset timer */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 386 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 387 | break; |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 388 | case _NSVC_TIMER_NR: |
| 389 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 390 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* Section 9.2.6 */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 394 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 395 | { |
| 396 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 397 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 398 | uint16_t nsvci, nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 399 | |
| 400 | if (!msg) |
| 401 | return -ENOMEM; |
| 402 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 403 | nsvci = htons(nsvc->nsvci); |
| 404 | nsei = htons(nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 405 | |
| 406 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 407 | |
| 408 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 409 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 410 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 411 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 412 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 413 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 414 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 415 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 416 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 417 | } |
| 418 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 419 | /* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */ |
| 420 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 421 | { |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 422 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 423 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 424 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 425 | |
| 426 | nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg)); |
| 427 | if (!nsvc) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 428 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 429 | "to NS-VC!\n", msgb_nsei(msg)); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 430 | return -EINVAL; |
| 431 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 432 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 433 | if (!(nsvc->state & NSE_S_ALIVE)) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 434 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 435 | nsvc->nsei); |
| 436 | return -EBUSY; |
| 437 | } |
| 438 | if (nsvc->state & NSE_S_BLOCKED) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 439 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 440 | nsvc->nsei); |
| 441 | return -EBUSY; |
| 442 | } |
| 443 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 444 | nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3); |
| 445 | if (!nsh) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 446 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 447 | return -EIO; |
| 448 | } |
| 449 | |
| 450 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 451 | /* spare octet in data[0] */ |
| 452 | nsh->data[1] = bvci >> 8; |
| 453 | nsh->data[2] = bvci & 0xff; |
| 454 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 455 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* Section 9.2.10: receive side */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 459 | static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 460 | { |
| 461 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 462 | uint16_t bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 463 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 464 | if (nsvc->state & NSE_S_BLOCKED) |
| 465 | return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED, |
| 466 | 0, msg); |
| 467 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 468 | /* spare octet in data[0] */ |
| 469 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 470 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 471 | msgb_bvci(msg) = bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 472 | |
| 473 | /* call upper layer (BSSGP) */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 474 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* Section 9.2.7 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 478 | static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 479 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 480 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 481 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 482 | uint8_t cause; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 483 | int rc; |
| 484 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 485 | LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 486 | |
| 487 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 488 | |
| 489 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 490 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 491 | return -EINVAL; |
| 492 | } |
| 493 | |
| 494 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 495 | LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | /* Section 7.3 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 501 | static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 502 | { |
| 503 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 504 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 505 | uint8_t *cause; |
| 506 | uint16_t *nsvci, *nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 507 | int rc; |
| 508 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 509 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 510 | |
| 511 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 512 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 513 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 514 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 515 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 516 | return -EINVAL; |
| 517 | } |
| 518 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 519 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 520 | nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 521 | nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 522 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 523 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 524 | nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause)); |
| 525 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 526 | /* Mark NS-VC as blocked and alive */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 527 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f44448d | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 528 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 529 | nsvc->nsei = ntohs(*nsei); |
| 530 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 531 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 532 | /* start the test procedure */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 533 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 534 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 535 | /* inform interested parties about the fact that this NSVC |
| 536 | * has received RESET */ |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 537 | ns_dispatch_signal(nsvc, S_NS_RESET, *cause); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 538 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 539 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 540 | } |
| 541 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 542 | static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 543 | { |
| 544 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 545 | struct tlv_parsed tp; |
| 546 | uint8_t *cause; |
| 547 | int rc; |
| 548 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 549 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 550 | |
| 551 | nsvc->state |= NSE_S_BLOCKED; |
| 552 | |
| 553 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 554 | |
| 555 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 556 | !TLVP_PRESENT(&tp, NS_IE_VCI)) { |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 557 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 558 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 559 | return -EINVAL; |
| 560 | } |
| 561 | |
| 562 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 563 | //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 564 | |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 565 | ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 566 | |
| 567 | return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK); |
| 568 | } |
| 569 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 570 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 571 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 572 | struct sockaddr_in *saddr) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 573 | { |
| 574 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 575 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 576 | int rc = 0; |
| 577 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 578 | /* look up the NSVC based on source address */ |
| 579 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 580 | if (!nsvc) { |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 581 | struct tlv_parsed tp; |
| 582 | uint16_t nsei; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 583 | /* Only the RESET procedure creates a new NSVC */ |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 584 | if (nsh->pdu_type != NS_PDUT_RESET) { |
Harald Welte | 78681af | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 585 | struct gprs_nsvc fake_nsvc; |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 586 | LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x " |
Harald Welte | c8c3de3 | 2010-05-11 18:38:36 +0200 | [diff] [blame] | 587 | "from %s:%u for non-existing NS-VC\n", |
| 588 | nsh->pdu_type, inet_ntoa(saddr->sin_addr), |
| 589 | ntohs(saddr->sin_port)); |
Harald Welte | 78681af | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 590 | /* Since we have no NSVC, we have to create a fake */ |
| 591 | fake_nsvc.nsvci = fake_nsvc.nsei = 0; |
| 592 | fake_nsvc.nsi = nsi; |
| 593 | fake_nsvc.ip.bts_addr = *saddr; |
| 594 | fake_nsvc.state = NSE_S_ALIVE; |
| 595 | #if 0 |
| 596 | return gprs_ns_tx_status(&fake_nsvc, |
| 597 | NS_CAUSE_PDU_INCOMP_PSTATE, 0, |
| 598 | msg); |
| 599 | #else |
| 600 | /* BS+ Gb implementation ignores STATUS, so we try |
| 601 | * our luck with a RESET incompatible with the spec */ |
| 602 | return gprs_ns_tx_simple(&fake_nsvc, NS_PDUT_RESET); |
| 603 | #endif |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 604 | } |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 605 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 606 | msgb_l2len(msg), 0, 0); |
| 607 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 608 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 609 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 610 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 611 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
| 612 | msg); |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 613 | return -EINVAL; |
| 614 | } |
| 615 | nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI)); |
| 616 | /* Check if we already know this NSEI, the remote end might |
| 617 | * simply have changed addresses, or it is a SGSN */ |
| 618 | nsvc = nsvc_by_nsei(nsi, nsei); |
| 619 | if (!nsvc) { |
| 620 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n", |
| 621 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
| 622 | nsvc = nsvc_create(nsi, 0xffff); |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 623 | } |
Harald Welte | 19b96b9 | 2010-05-11 18:40:45 +0200 | [diff] [blame] | 624 | /* Update the remote peer IP address/port */ |
| 625 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 626 | } else |
| 627 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 628 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 629 | switch (nsh->pdu_type) { |
| 630 | case NS_PDUT_ALIVE: |
| 631 | /* remote end inquires whether we're still alive, |
| 632 | * we need to respond with ALIVE_ACK */ |
Harald Welte | 9a4847f | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 633 | rc = gprs_ns_tx_alive_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 634 | break; |
| 635 | case NS_PDUT_ALIVE_ACK: |
| 636 | /* stop Tns-alive */ |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 637 | bsc_del_timer(&nsvc->timer); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 638 | /* start Tns-test */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 639 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 1bb9e79 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 640 | if (nsvc->remote_end_is_sgsn) { |
| 641 | /* FIXME: this should be one level higher */ |
| 642 | if (nsvc->state & NSE_S_BLOCKED) |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 643 | rc = gprs_ns_tx_unblock(nsvc); |
Harald Welte | 1bb9e79 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 644 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 645 | break; |
| 646 | case NS_PDUT_UNITDATA: |
| 647 | /* actual user data */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 648 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 649 | break; |
| 650 | case NS_PDUT_STATUS: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 651 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 652 | break; |
| 653 | case NS_PDUT_RESET: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 654 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 655 | break; |
| 656 | case NS_PDUT_RESET_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 657 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 658 | /* mark NS-VC as blocked + active */ |
| 659 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 660 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 661 | if (nsvc->remote_end_is_sgsn) { |
| 662 | /* stop RESET timer */ |
| 663 | bsc_del_timer(&nsvc->timer); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 664 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 665 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 666 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 667 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 668 | break; |
| 669 | case NS_PDUT_UNBLOCK: |
| 670 | /* Section 7.2: unblocking procedure */ |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 671 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 672 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 673 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 674 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 675 | break; |
| 676 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 677 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 678 | /* mark NS-VC as unblocked + active */ |
| 679 | nsvc->state = NSE_S_ALIVE; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 680 | nsvc->remote_state = NSE_S_ALIVE; |
Harald Welte | 4108b38 | 2010-05-12 14:18:46 +0000 | [diff] [blame^] | 681 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 682 | break; |
| 683 | case NS_PDUT_BLOCK: |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 684 | rc = gprs_ns_rx_block(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 685 | break; |
| 686 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 687 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 688 | /* mark remote NS-VC as blocked + active */ |
| 689 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 690 | break; |
| 691 | default: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 692 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 693 | nsvc->nsei, nsh->pdu_type); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 694 | rc = -EINVAL; |
| 695 | break; |
| 696 | } |
| 697 | return rc; |
| 698 | } |
| 699 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 700 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb) |
| 701 | { |
| 702 | struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst); |
| 703 | |
| 704 | nsi->cb = cb; |
| 705 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
| 706 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 707 | return nsi; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 711 | { |
| 712 | /* FIXME: clear all timers */ |
| 713 | |
| 714 | /* recursively free the NSI and all its NSVCs */ |
| 715 | talloc_free(nsi); |
| 716 | } |
| 717 | |
| 718 | |
| 719 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 720 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 721 | |
| 722 | /* Read a single NS-over-IP message */ |
| 723 | static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error, |
| 724 | struct sockaddr_in *saddr) |
| 725 | { |
| 726 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS"); |
| 727 | int ret = 0; |
| 728 | socklen_t saddr_len = sizeof(*saddr); |
| 729 | |
| 730 | if (!msg) { |
| 731 | *error = -ENOMEM; |
| 732 | return NULL; |
| 733 | } |
| 734 | |
| 735 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 736 | (struct sockaddr *)saddr, &saddr_len); |
| 737 | if (ret < 0) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 738 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 739 | strerror(errno)); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 740 | msgb_free(msg); |
| 741 | *error = ret; |
| 742 | return NULL; |
| 743 | } else if (ret == 0) { |
| 744 | msgb_free(msg); |
| 745 | *error = ret; |
| 746 | return NULL; |
| 747 | } |
| 748 | |
| 749 | msg->l2h = msg->data; |
| 750 | msgb_put(msg, ret); |
| 751 | |
| 752 | return msg; |
| 753 | } |
| 754 | |
| 755 | static int handle_nsip_read(struct bsc_fd *bfd) |
| 756 | { |
| 757 | int error; |
| 758 | struct sockaddr_in saddr; |
| 759 | struct gprs_ns_inst *nsi = bfd->data; |
| 760 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 761 | |
| 762 | if (!msg) |
| 763 | return error; |
| 764 | |
| 765 | return gprs_ns_rcvmsg(nsi, msg, &saddr); |
| 766 | } |
| 767 | |
| 768 | static int handle_nsip_write(struct bsc_fd *bfd) |
| 769 | { |
| 770 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 771 | return -EIO; |
| 772 | } |
| 773 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 774 | int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 775 | { |
| 776 | int rc; |
| 777 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 778 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 779 | |
| 780 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 781 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 782 | |
| 783 | talloc_free(msg); |
| 784 | |
| 785 | return rc; |
| 786 | } |
| 787 | |
| 788 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
| 789 | static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 790 | { |
| 791 | int rc = 0; |
| 792 | |
| 793 | if (what & BSC_FD_READ) |
| 794 | rc = handle_nsip_read(bfd); |
| 795 | if (what & BSC_FD_WRITE) |
| 796 | rc = handle_nsip_write(bfd); |
| 797 | |
| 798 | return rc; |
| 799 | } |
| 800 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 801 | extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port, |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 802 | int (*cb)(struct bsc_fd *fd, unsigned int what)); |
| 803 | |
| 804 | /* Listen for incoming GPRS packets */ |
| 805 | int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port) |
| 806 | { |
| 807 | int ret; |
| 808 | |
| 809 | ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb); |
| 810 | if (ret < 0) |
| 811 | return ret; |
| 812 | |
| 813 | nsi->ll = GPRS_NS_LL_UDP; |
| 814 | nsi->nsip.fd.data = nsi; |
| 815 | |
| 816 | return ret; |
| 817 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 818 | |
| 819 | /* Establish a connection (from the BSS) to the SGSN */ |
| 820 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 821 | struct sockaddr_in *dest, uint16_t nsei, |
| 822 | uint16_t nsvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 823 | { |
| 824 | struct gprs_nsvc *nsvc; |
| 825 | |
| 826 | nsvc = nsvc_by_rem_addr(nsi, dest); |
Harald Welte | 517b77f | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 827 | if (!nsvc) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 828 | nsvc = nsvc_create(nsi, nsvci); |
Harald Welte | 517b77f | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 829 | nsvc->ip.bts_addr = *dest; |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 830 | nsvc->nsei = nsei; |
| 831 | nsvc->nsvci = nsvci; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 832 | nsvc->remote_end_is_sgsn = 1; |
| 833 | |
| 834 | /* Initiate a RESET procedure */ |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 835 | /* Mark NS-VC locally as blocked and dead */ |
| 836 | nsvc->state = NSE_S_BLOCKED; |
| 837 | /* Send NS-RESET PDU */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 838 | if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 839 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 840 | nsei); |
| 841 | } |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 842 | /* Start Tns-reset */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 843 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 844 | |
| 845 | return nsvc; |
| 846 | } |