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