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