Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS Networks Service (NS) messages on the Gb interface |
| 2 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */ |
| 3 | |
| 4 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 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> |
| 50 | #include <sys/types.h> |
| 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> |
| 58 | #include <openbsc/debug.h> |
| 59 | #include <openbsc/gprs_ns.h> |
| 60 | #include <openbsc/gprs_bssgp.h> |
| 61 | |
| 62 | #define NS_ALLOC_SIZE 1024 |
| 63 | |
| 64 | static const struct tlv_definition ns_att_tlvdef = { |
| 65 | .def = { |
| 66 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 67 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 68 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 69 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 70 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | #define NSE_S_BLOCKED 0x0001 |
| 75 | #define NSE_S_ALIVE 0x0002 |
| 76 | |
| 77 | struct gprs_nsvc { |
| 78 | struct llist_head list; |
| 79 | |
| 80 | u_int16_t nsei; /* end-to-end significance */ |
| 81 | u_int16_t nsvci; /* uniquely identifies NS-VC at SGSN */ |
| 82 | |
| 83 | u_int32_t state; |
| 84 | |
| 85 | struct timer_list alive_timer; |
| 86 | int timer_is_tns_alive; |
| 87 | int alive_retries; |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 88 | |
| 89 | union { |
| 90 | struct { |
| 91 | struct sockaddr_in bts_addr; |
| 92 | } ip; |
| 93 | }; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /* FIXME: dynamically search for the matching NSVC */ |
| 97 | static struct gprs_nsvc dummy_nsvc = { .state = NSE_S_BLOCKED | NSE_S_ALIVE }; |
| 98 | |
| 99 | /* Section 10.3.2, Table 13 */ |
| 100 | static const char *ns_cause_str[] = { |
| 101 | [NS_CAUSE_TRANSIT_FAIL] = "Transit network failure", |
| 102 | [NS_CAUSE_OM_INTERVENTION] = "O&M intervention", |
| 103 | [NS_CAUSE_EQUIP_FAIL] = "Equipment failure", |
| 104 | [NS_CAUSE_NSVC_BLOCKED] = "NS-VC blocked", |
| 105 | [NS_CAUSE_NSVC_UNKNOWN] = "NS-VC unknown", |
| 106 | [NS_CAUSE_BVCI_UNKNOWN] = "BVCI unknown", |
| 107 | [NS_CAUSE_SEM_INCORR_PDU] = "Semantically incorrect PDU", |
| 108 | [NS_CAUSE_PDU_INCOMP_PSTATE] = "PDU not compatible with protocol state", |
| 109 | [NS_CAUSE_PROTO_ERR_UNSPEC] = "Protocol error, unspecified", |
| 110 | [NS_CAUSE_INVAL_ESSENT_IE] = "Invalid essential IE", |
| 111 | [NS_CAUSE_MISSING_ESSENT_IE] = "Missing essential IE", |
| 112 | }; |
| 113 | |
| 114 | static const char *gprs_ns_cause_str(enum ns_cause cause) |
| 115 | { |
| 116 | if (cause >= ARRAY_SIZE(ns_cause_str)) |
| 117 | return "undefined"; |
| 118 | |
| 119 | if (ns_cause_str[cause]) |
| 120 | return ns_cause_str[cause]; |
| 121 | |
| 122 | return "undefined"; |
| 123 | } |
| 124 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 125 | static int gprs_ns_tx(struct msgb *msg, struct gprs_nsvc *nsvc) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 126 | { |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 127 | return ipac_gprs_send(msg, &nsvc->ip.bts_addr); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 128 | } |
| 129 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 130 | static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, u_int8_t pdu_type) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 131 | { |
| 132 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 133 | struct gprs_ns_hdr *nsh; |
| 134 | |
| 135 | if (!msg) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 139 | |
| 140 | nsh->pdu_type = pdu_type; |
| 141 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 142 | return gprs_ns_tx(msg, nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #define NS_TIMER_ALIVE 3, 0 /* after 3 seconds without response, we retry */ |
| 146 | #define NS_TIMER_TEST 30, 0 /* every 10 seconds we check if the BTS is still alive */ |
| 147 | #define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */ |
| 148 | |
| 149 | static void gprs_ns_alive_cb(void *data) |
| 150 | { |
| 151 | struct gprs_nsvc *nsvc = data; |
| 152 | |
| 153 | if (nsvc->timer_is_tns_alive) { |
| 154 | /* Tns-alive case: we expired without response ! */ |
| 155 | nsvc->alive_retries++; |
| 156 | if (nsvc->alive_retries > NS_ALIVE_RETRIES) { |
| 157 | /* mark as dead and blocked */ |
| 158 | nsvc->state = NSE_S_BLOCKED; |
| 159 | DEBUGP(DGPRS, "Tns-alive more then %u retries, " |
| 160 | " blocking NS-VC\n", NS_ALIVE_RETRIES); |
| 161 | /* FIXME: inform higher layers */ |
| 162 | return; |
| 163 | } |
| 164 | } else { |
| 165 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 166 | gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 167 | /* start Tns-alive timer */ |
| 168 | nsvc->timer_is_tns_alive = 1; |
| 169 | } |
| 170 | bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE); |
| 171 | } |
| 172 | |
| 173 | /* Section 9.2.6 */ |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 174 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 175 | { |
| 176 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 177 | struct gprs_ns_hdr *nsh; |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 178 | u_int16_t nsvci, nsei; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 179 | |
| 180 | if (!msg) |
| 181 | return -ENOMEM; |
| 182 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 183 | nsvci = htons(nsvc->nsvci); |
| 184 | nsei = htons(nsvc->nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 185 | |
| 186 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 187 | |
| 188 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 189 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 190 | DEBUGP(DGPRS, "nsvci=%u, nsei=%u\n", nsvc->nsvci, nsvc->nsei); |
| 191 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 192 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (u_int8_t *)&nsvci); |
| 193 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (u_int8_t *)&nsei); |
| 194 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 195 | return gprs_ns_tx(msg, nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Section 9.2.10: transmit side */ |
| 199 | int gprs_ns_sendmsg(struct gprs_ns_link *link, u_int16_t bvci, |
| 200 | struct msgb *msg) |
| 201 | { |
| 202 | struct gprs_ns_hdr *nsh; |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 203 | struct gprs_nsvc *nsvc = &dummy_nsvc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 204 | |
| 205 | nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3); |
| 206 | if (!nsh) { |
| 207 | DEBUGP(DGPRS, "Not enough headroom for NS header\n"); |
| 208 | return -EIO; |
| 209 | } |
| 210 | |
| 211 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 212 | /* spare octet in data[0] */ |
| 213 | nsh->data[1] = bvci >> 8; |
| 214 | nsh->data[2] = bvci & 0xff; |
| 215 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 216 | return gprs_ns_tx(msg, nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* Section 9.2.10: receive side */ |
| 220 | static int gprs_ns_rx_unitdata(struct msgb *msg) |
| 221 | { |
| 222 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
| 223 | u_int16_t bvci; |
| 224 | |
| 225 | /* spare octet in data[0] */ |
| 226 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
| 227 | msg->l3h = &nsh->data[3]; |
| 228 | |
| 229 | /* call upper layer (BSSGP) */ |
| 230 | return gprs_bssgp_rcvmsg(msg, bvci); |
| 231 | } |
| 232 | |
| 233 | /* Section 9.2.7 */ |
| 234 | static int gprs_ns_rx_status(struct msgb *msg) |
| 235 | { |
| 236 | struct gprs_ns_hdr *nsh = msg->l2h; |
| 237 | struct tlv_parsed tp; |
| 238 | u_int8_t cause; |
| 239 | int rc; |
| 240 | |
| 241 | DEBUGP(DGPRS, "NS STATUS "); |
| 242 | |
| 243 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 244 | |
| 245 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
| 246 | DEBUGPC(DGPRS, "missing cause IE\n"); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
| 251 | DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause)); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* Section 7.3 */ |
| 257 | static int gprs_ns_rx_reset(struct msgb *msg) |
| 258 | { |
| 259 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 260 | struct gprs_nsvc *nsvc = &dummy_nsvc; |
| 261 | struct tlv_parsed tp; |
| 262 | u_int8_t *cause; |
| 263 | u_int16_t *nsvci, *nsei; |
| 264 | int rc; |
| 265 | |
| 266 | DEBUGP(DGPRS, "NS RESET "); |
| 267 | |
| 268 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 269 | |
| 270 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 271 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 272 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
| 273 | /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */ |
| 274 | DEBUGPC(DGPRS, "Missing mandatory IE\n"); |
| 275 | return -EINVAL; |
| 276 | } |
| 277 | |
| 278 | cause = (u_int8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 279 | nsvci = (u_int16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 280 | nsei = (u_int16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
| 281 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 282 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
| 283 | nsvc->nsei = ntohs(*nsei); |
| 284 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 285 | |
| 286 | DEBUGPC(DGPRS, "cause=%s, NSVCI=%u, NSEI=%u\n", |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 287 | gprs_ns_cause_str(*cause), nsvc->nsvci, nsvc->nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 288 | |
| 289 | /* mark the NS-VC as blocked and alive */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 290 | /* start the test procedure */ |
| 291 | nsvc->alive_timer.cb = gprs_ns_alive_cb; |
| 292 | nsvc->alive_timer.data = nsvc; |
| 293 | bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE); |
| 294 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 295 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 299 | int gprs_ns_rcvmsg(struct msgb *msg, struct sockaddr_in *saddr) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 300 | { |
| 301 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 302 | struct gprs_nsvc *nsvc = &dummy_nsvc; |
| 303 | int rc = 0; |
| 304 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 305 | /* FIXME: do this properly! */ |
| 306 | nsvc->ip.bts_addr = *saddr; |
| 307 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 308 | switch (nsh->pdu_type) { |
| 309 | case NS_PDUT_ALIVE: |
| 310 | /* remote end inquires whether we're still alive, |
| 311 | * we need to respond with ALIVE_ACK */ |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 312 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 313 | break; |
| 314 | case NS_PDUT_ALIVE_ACK: |
| 315 | /* stop Tns-alive */ |
| 316 | bsc_del_timer(&nsvc->alive_timer); |
| 317 | /* start Tns-test */ |
| 318 | nsvc->timer_is_tns_alive = 0; |
| 319 | bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_TEST); |
| 320 | break; |
| 321 | case NS_PDUT_UNITDATA: |
| 322 | /* actual user data */ |
| 323 | rc = gprs_ns_rx_unitdata(msg); |
| 324 | break; |
| 325 | case NS_PDUT_STATUS: |
| 326 | rc = gprs_ns_rx_status(msg); |
| 327 | break; |
| 328 | case NS_PDUT_RESET: |
| 329 | rc = gprs_ns_rx_reset(msg); |
| 330 | break; |
| 331 | case NS_PDUT_RESET_ACK: |
| 332 | /* FIXME: mark remote NS-VC as blocked + active */ |
| 333 | break; |
| 334 | case NS_PDUT_UNBLOCK: |
| 335 | /* Section 7.2: unblocking procedure */ |
| 336 | DEBUGP(DGPRS, "NS UNBLOCK\n"); |
| 337 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 338 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 339 | break; |
| 340 | case NS_PDUT_UNBLOCK_ACK: |
| 341 | /* FIXME: mark remote NS-VC as unblocked + active */ |
| 342 | break; |
| 343 | case NS_PDUT_BLOCK: |
| 344 | DEBUGP(DGPRS, "NS BLOCK\n"); |
| 345 | nsvc->state |= NSE_S_BLOCKED; |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame^] | 346 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 347 | break; |
| 348 | case NS_PDUT_BLOCK_ACK: |
| 349 | /* FIXME: mark remote NS-VC as blocked + active */ |
| 350 | break; |
| 351 | default: |
| 352 | DEBUGP(DGPRS, "Unknown NS PDU type 0x%02x\n", nsh->pdu_type); |
| 353 | rc = -EINVAL; |
| 354 | break; |
| 355 | } |
| 356 | return rc; |
| 357 | } |
| 358 | |