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> |
| 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> |
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> |
| 60 | #include <openbsc/gprs_ns.h> |
| 61 | #include <openbsc/gprs_bssgp.h> |
| 62 | |
| 63 | #define NS_ALLOC_SIZE 1024 |
| 64 | |
| 65 | static const struct tlv_definition ns_att_tlvdef = { |
| 66 | .def = { |
| 67 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 68 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 69 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 70 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 71 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 72 | }, |
| 73 | }; |
| 74 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 75 | enum gprs_ns_ll { |
| 76 | GPRS_NS_LL_UDP, |
| 77 | GPRS_NS_LL_E1, |
| 78 | }; |
| 79 | |
| 80 | /* An instance of the NS protocol stack */ |
| 81 | struct gprs_ns_inst { |
| 82 | /* callback to the user for incoming UNIT DATA IND */ |
| 83 | gprs_ns_cb_t *cb; |
| 84 | |
| 85 | /* linked lists of all NSVC in this instance */ |
| 86 | struct llist_head gprs_nsvcs; |
| 87 | |
| 88 | /* which link-layer are we based on? */ |
| 89 | enum gprs_ns_ll ll; |
| 90 | |
| 91 | union { |
| 92 | /* NS-over-IP specific bits */ |
| 93 | struct { |
| 94 | struct bsc_fd fd; |
| 95 | } nsip; |
| 96 | }; |
| 97 | }; |
| 98 | |
| 99 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 100 | static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, |
| 101 | u_int16_t nsvci) |
| 102 | { |
| 103 | struct gprs_nsvc *nsvc; |
| 104 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 105 | if (nsvc->nsvci == nsvci) |
| 106 | return nsvc; |
| 107 | } |
| 108 | return NULL; |
| 109 | } |
| 110 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 111 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 112 | static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, |
| 113 | u_int16_t nsei) |
| 114 | { |
| 115 | struct gprs_nsvc *nsvc; |
| 116 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 117 | if (nsvc->nsei == nsei) |
| 118 | return nsvc; |
| 119 | } |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 124 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 125 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 126 | struct sockaddr_in *sin) |
| 127 | { |
| 128 | struct gprs_nsvc *nsvc; |
| 129 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 130 | if (!memcmp(&nsvc->ip.bts_addr, sin, sizeof(*sin))) |
| 131 | return nsvc; |
| 132 | } |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | static struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, u_int16_t nsvci) |
| 137 | { |
| 138 | struct gprs_nsvc *nsvc; |
| 139 | |
| 140 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 141 | nsvc->nsvci = nsvci; |
| 142 | /* before RESET procedure: BLOCKED and DEAD */ |
| 143 | nsvc->state = NSE_S_BLOCKED; |
| 144 | nsvc->nsi = nsi; |
| 145 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 146 | |
| 147 | return nsvc; |
| 148 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 149 | |
| 150 | /* Section 10.3.2, Table 13 */ |
| 151 | static const char *ns_cause_str[] = { |
| 152 | [NS_CAUSE_TRANSIT_FAIL] = "Transit network failure", |
| 153 | [NS_CAUSE_OM_INTERVENTION] = "O&M intervention", |
| 154 | [NS_CAUSE_EQUIP_FAIL] = "Equipment failure", |
| 155 | [NS_CAUSE_NSVC_BLOCKED] = "NS-VC blocked", |
| 156 | [NS_CAUSE_NSVC_UNKNOWN] = "NS-VC unknown", |
| 157 | [NS_CAUSE_BVCI_UNKNOWN] = "BVCI unknown", |
| 158 | [NS_CAUSE_SEM_INCORR_PDU] = "Semantically incorrect PDU", |
| 159 | [NS_CAUSE_PDU_INCOMP_PSTATE] = "PDU not compatible with protocol state", |
| 160 | [NS_CAUSE_PROTO_ERR_UNSPEC] = "Protocol error, unspecified", |
| 161 | [NS_CAUSE_INVAL_ESSENT_IE] = "Invalid essential IE", |
| 162 | [NS_CAUSE_MISSING_ESSENT_IE] = "Missing essential IE", |
| 163 | }; |
| 164 | |
| 165 | static const char *gprs_ns_cause_str(enum ns_cause cause) |
| 166 | { |
| 167 | if (cause >= ARRAY_SIZE(ns_cause_str)) |
| 168 | return "undefined"; |
| 169 | |
| 170 | if (ns_cause_str[cause]) |
| 171 | return ns_cause_str[cause]; |
| 172 | |
| 173 | return "undefined"; |
| 174 | } |
| 175 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 176 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 177 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 178 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 179 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 180 | int ret; |
| 181 | |
| 182 | switch (nsvc->nsi->ll) { |
| 183 | case GPRS_NS_LL_UDP: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 184 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 185 | break; |
| 186 | default: |
| 187 | LOGP(DGPRS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll); |
| 188 | msgb_free(msg); |
| 189 | ret = -EIO; |
| 190 | break; |
| 191 | } |
| 192 | return ret; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 193 | } |
| 194 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 195 | static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, u_int8_t pdu_type) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 196 | { |
| 197 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 198 | struct gprs_ns_hdr *nsh; |
| 199 | |
| 200 | if (!msg) |
| 201 | return -ENOMEM; |
| 202 | |
| 203 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 204 | |
| 205 | nsh->pdu_type = pdu_type; |
| 206 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 207 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | #define NS_TIMER_ALIVE 3, 0 /* after 3 seconds without response, we retry */ |
| 211 | #define NS_TIMER_TEST 30, 0 /* every 10 seconds we check if the BTS is still alive */ |
| 212 | #define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */ |
| 213 | |
| 214 | static void gprs_ns_alive_cb(void *data) |
| 215 | { |
| 216 | struct gprs_nsvc *nsvc = data; |
| 217 | |
| 218 | if (nsvc->timer_is_tns_alive) { |
| 219 | /* Tns-alive case: we expired without response ! */ |
| 220 | nsvc->alive_retries++; |
| 221 | if (nsvc->alive_retries > NS_ALIVE_RETRIES) { |
| 222 | /* mark as dead and blocked */ |
| 223 | nsvc->state = NSE_S_BLOCKED; |
| 224 | DEBUGP(DGPRS, "Tns-alive more then %u retries, " |
| 225 | " blocking NS-VC\n", NS_ALIVE_RETRIES); |
| 226 | /* FIXME: inform higher layers */ |
| 227 | return; |
| 228 | } |
| 229 | } else { |
| 230 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 231 | gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 232 | /* start Tns-alive timer */ |
| 233 | nsvc->timer_is_tns_alive = 1; |
| 234 | } |
| 235 | bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE); |
| 236 | } |
| 237 | |
| 238 | /* Section 9.2.6 */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 239 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 240 | { |
| 241 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 242 | struct gprs_ns_hdr *nsh; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 243 | u_int16_t nsvci, nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 244 | |
| 245 | if (!msg) |
| 246 | return -ENOMEM; |
| 247 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 248 | nsvci = htons(nsvc->nsvci); |
| 249 | nsei = htons(nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 250 | |
| 251 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 252 | |
| 253 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 254 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 255 | DEBUGP(DGPRS, "nsvci=%u, nsei=%u\n", nsvc->nsvci, nsvc->nsei); |
| 256 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 257 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (u_int8_t *)&nsvci); |
| 258 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (u_int8_t *)&nsei); |
| 259 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 260 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 261 | } |
| 262 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 263 | /* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */ |
| 264 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 265 | { |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 266 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 267 | struct gprs_ns_hdr *nsh; |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 268 | u_int16_t bvci = msgb_bvci(msg); |
| 269 | |
| 270 | nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg)); |
| 271 | if (!nsvc) { |
| 272 | DEBUGP(DGPRS, "Unable to resolve NSEI %u to NS-VC!\n", msgb_nsei(msg)); |
| 273 | return -EINVAL; |
| 274 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 275 | |
| 276 | nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3); |
| 277 | if (!nsh) { |
| 278 | DEBUGP(DGPRS, "Not enough headroom for NS header\n"); |
| 279 | return -EIO; |
| 280 | } |
| 281 | |
| 282 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 283 | /* spare octet in data[0] */ |
| 284 | nsh->data[1] = bvci >> 8; |
| 285 | nsh->data[2] = bvci & 0xff; |
| 286 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 287 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* Section 9.2.10: receive side */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
| 294 | u_int16_t bvci; |
| 295 | |
| 296 | /* spare octet in data[0] */ |
| 297 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
| 298 | msg->l3h = &nsh->data[3]; |
| 299 | |
| 300 | /* call upper layer (BSSGP) */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 301 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* Section 9.2.7 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 305 | 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] | 306 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 307 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 308 | struct tlv_parsed tp; |
| 309 | u_int8_t cause; |
| 310 | int rc; |
| 311 | |
| 312 | DEBUGP(DGPRS, "NS STATUS "); |
| 313 | |
| 314 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 315 | |
| 316 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
| 317 | DEBUGPC(DGPRS, "missing cause IE\n"); |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
| 322 | DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause)); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* Section 7.3 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 328 | 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] | 329 | { |
| 330 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 331 | struct tlv_parsed tp; |
| 332 | u_int8_t *cause; |
| 333 | u_int16_t *nsvci, *nsei; |
| 334 | int rc; |
| 335 | |
| 336 | DEBUGP(DGPRS, "NS RESET "); |
| 337 | |
| 338 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 339 | |
| 340 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 341 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 342 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
| 343 | /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */ |
| 344 | DEBUGPC(DGPRS, "Missing mandatory IE\n"); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | cause = (u_int8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 349 | nsvci = (u_int16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 350 | nsei = (u_int16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
| 351 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 352 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
| 353 | nsvc->nsei = ntohs(*nsei); |
| 354 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 355 | |
| 356 | DEBUGPC(DGPRS, "cause=%s, NSVCI=%u, NSEI=%u\n", |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 357 | gprs_ns_cause_str(*cause), nsvc->nsvci, nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 358 | |
| 359 | /* mark the NS-VC as blocked and alive */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 360 | /* start the test procedure */ |
| 361 | nsvc->alive_timer.cb = gprs_ns_alive_cb; |
| 362 | nsvc->alive_timer.data = nsvc; |
| 363 | bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_ALIVE); |
| 364 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 365 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 369 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 370 | struct sockaddr_in *saddr) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 371 | { |
| 372 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 373 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 374 | int rc = 0; |
| 375 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 376 | /* look up the NSVC based on source address */ |
| 377 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 378 | if (!nsvc) { |
| 379 | /* Only the RESET procedure creates a new NSVC */ |
| 380 | if (nsh->pdu_type != NS_PDUT_RESET) |
| 381 | return -EIO; |
| 382 | nsvc = nsvc_create(nsi, 0xffff); |
| 383 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 384 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 385 | return rc; |
| 386 | } |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 387 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 388 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 389 | switch (nsh->pdu_type) { |
| 390 | case NS_PDUT_ALIVE: |
| 391 | /* remote end inquires whether we're still alive, |
| 392 | * we need to respond with ALIVE_ACK */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 393 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 394 | break; |
| 395 | case NS_PDUT_ALIVE_ACK: |
| 396 | /* stop Tns-alive */ |
| 397 | bsc_del_timer(&nsvc->alive_timer); |
| 398 | /* start Tns-test */ |
| 399 | nsvc->timer_is_tns_alive = 0; |
| 400 | bsc_schedule_timer(&nsvc->alive_timer, NS_TIMER_TEST); |
| 401 | break; |
| 402 | case NS_PDUT_UNITDATA: |
| 403 | /* actual user data */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 404 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 405 | break; |
| 406 | case NS_PDUT_STATUS: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 407 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 408 | break; |
| 409 | case NS_PDUT_RESET: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 410 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 411 | break; |
| 412 | case NS_PDUT_RESET_ACK: |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 413 | DEBUGP(DGPRS, "NS RESET ACK\n"); |
| 414 | /* mark remote NS-VC as blocked + active */ |
| 415 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 416 | break; |
| 417 | case NS_PDUT_UNBLOCK: |
| 418 | /* Section 7.2: unblocking procedure */ |
| 419 | DEBUGP(DGPRS, "NS UNBLOCK\n"); |
| 420 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 421 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 422 | break; |
| 423 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 424 | DEBUGP(DGPRS, "NS UNBLOCK ACK\n"); |
| 425 | /* mark remote NS-VC as unblocked + active */ |
| 426 | nsvc->remote_state = NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 427 | break; |
| 428 | case NS_PDUT_BLOCK: |
| 429 | DEBUGP(DGPRS, "NS BLOCK\n"); |
| 430 | nsvc->state |= NSE_S_BLOCKED; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 431 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 432 | break; |
| 433 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 434 | DEBUGP(DGPRS, "NS BLOCK ACK\n"); |
| 435 | /* mark remote NS-VC as blocked + active */ |
| 436 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 437 | break; |
| 438 | default: |
| 439 | DEBUGP(DGPRS, "Unknown NS PDU type 0x%02x\n", nsh->pdu_type); |
| 440 | rc = -EINVAL; |
| 441 | break; |
| 442 | } |
| 443 | return rc; |
| 444 | } |
| 445 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 446 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb) |
| 447 | { |
| 448 | struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst); |
| 449 | |
| 450 | nsi->cb = cb; |
| 451 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
| 452 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame^] | 453 | return nsi; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 457 | { |
| 458 | /* FIXME: clear all timers */ |
| 459 | |
| 460 | /* recursively free the NSI and all its NSVCs */ |
| 461 | talloc_free(nsi); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 466 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 467 | |
| 468 | /* Read a single NS-over-IP message */ |
| 469 | static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error, |
| 470 | struct sockaddr_in *saddr) |
| 471 | { |
| 472 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS"); |
| 473 | int ret = 0; |
| 474 | socklen_t saddr_len = sizeof(*saddr); |
| 475 | |
| 476 | if (!msg) { |
| 477 | *error = -ENOMEM; |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 482 | (struct sockaddr *)saddr, &saddr_len); |
| 483 | if (ret < 0) { |
| 484 | fprintf(stderr, "recv error %s\n", strerror(errno)); |
| 485 | msgb_free(msg); |
| 486 | *error = ret; |
| 487 | return NULL; |
| 488 | } else if (ret == 0) { |
| 489 | msgb_free(msg); |
| 490 | *error = ret; |
| 491 | return NULL; |
| 492 | } |
| 493 | |
| 494 | msg->l2h = msg->data; |
| 495 | msgb_put(msg, ret); |
| 496 | |
| 497 | return msg; |
| 498 | } |
| 499 | |
| 500 | static int handle_nsip_read(struct bsc_fd *bfd) |
| 501 | { |
| 502 | int error; |
| 503 | struct sockaddr_in saddr; |
| 504 | struct gprs_ns_inst *nsi = bfd->data; |
| 505 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 506 | |
| 507 | if (!msg) |
| 508 | return error; |
| 509 | |
| 510 | return gprs_ns_rcvmsg(nsi, msg, &saddr); |
| 511 | } |
| 512 | |
| 513 | static int handle_nsip_write(struct bsc_fd *bfd) |
| 514 | { |
| 515 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 516 | return -EIO; |
| 517 | } |
| 518 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 519 | int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 520 | { |
| 521 | int rc; |
| 522 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 523 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 524 | |
| 525 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 526 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 527 | |
| 528 | talloc_free(msg); |
| 529 | |
| 530 | return rc; |
| 531 | } |
| 532 | |
| 533 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
| 534 | static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 535 | { |
| 536 | int rc = 0; |
| 537 | |
| 538 | if (what & BSC_FD_READ) |
| 539 | rc = handle_nsip_read(bfd); |
| 540 | if (what & BSC_FD_WRITE) |
| 541 | rc = handle_nsip_write(bfd); |
| 542 | |
| 543 | return rc; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | /* FIXME: this is currently in input/ipaccess.c */ |
| 548 | extern int make_sock(struct bsc_fd *bfd, int proto, u_int16_t port, |
| 549 | int (*cb)(struct bsc_fd *fd, unsigned int what)); |
| 550 | |
| 551 | /* Listen for incoming GPRS packets */ |
| 552 | int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port) |
| 553 | { |
| 554 | int ret; |
| 555 | |
| 556 | ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb); |
| 557 | if (ret < 0) |
| 558 | return ret; |
| 559 | |
| 560 | nsi->ll = GPRS_NS_LL_UDP; |
| 561 | nsi->nsip.fd.data = nsi; |
| 562 | |
| 563 | return ret; |
| 564 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame^] | 565 | |
| 566 | /* Establish a connection (from the BSS) to the SGSN */ |
| 567 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
| 568 | struct sockaddr_in *dest, uint16_t nsvci) |
| 569 | { |
| 570 | struct gprs_nsvc *nsvc; |
| 571 | |
| 572 | nsvc = nsvc_by_rem_addr(nsi, dest); |
| 573 | if (!nsvc) { |
| 574 | nsvc = nsvc_create(nsi, nsvci); |
| 575 | nsvc->ip.bts_addr = *dest; |
| 576 | } |
| 577 | nsvc->remote_end_is_sgsn = 1; |
| 578 | |
| 579 | /* Initiate a RESET procedure */ |
| 580 | if (gprs_ns_tx_simple(nsvc, NS_PDUT_RESET) < 0) |
| 581 | return NULL; |
| 582 | /* FIXME: should we run a timer and re-transmit the reset request? */ |
| 583 | |
| 584 | return nsvc; |
| 585 | } |