Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1 | /* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */ |
| 3 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 4 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | /* Some introduction into NS: NS is used typically on top of frame relay, |
| 25 | * but in the ip.access world it is encapsulated in UDP packets. It serves |
| 26 | * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't |
| 27 | * do much, apart from providing congestion notification and status indication. |
| 28 | * |
| 29 | * Terms: |
| 30 | * NS Network Service |
| 31 | * NSVC NS Virtual Connection |
| 32 | * NSEI NS Entity Identifier |
| 33 | * NSVL NS Virtual Link |
| 34 | * NSVLI NS Virtual Link Identifier |
| 35 | * BVC BSSGP Virtual Connection |
| 36 | * BVCI BSSGP Virtual Connection Identifier |
| 37 | * NSVCG NS Virtual Connection Goup |
| 38 | * Blocked NS-VC cannot be used for user traffic |
| 39 | * Alive Ability of a NS-VC to provide communication |
| 40 | * |
| 41 | * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will |
| 42 | * therefore identify the BSSGP virtual connection by a BVCI passed down to NS. |
| 43 | * NS then has to firgure out which NSVC's are responsible for this BVCI. |
| 44 | * Those mappings are administratively configured. |
| 45 | */ |
| 46 | |
| 47 | #include <stdlib.h> |
| 48 | #include <unistd.h> |
| 49 | #include <errno.h> |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 50 | #include <stdint.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 51 | |
| 52 | #include <arpa/inet.h> |
| 53 | |
| 54 | #include <openbsc/gsm_data.h> |
| 55 | #include <osmocore/msgb.h> |
| 56 | #include <osmocore/tlv.h> |
| 57 | #include <osmocore/talloc.h> |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 58 | #include <osmocore/select.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 59 | #include <openbsc/debug.h> |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 60 | #include <openbsc/signal.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 61 | #include <openbsc/gprs_ns.h> |
| 62 | #include <openbsc/gprs_bssgp.h> |
| 63 | |
| 64 | #define NS_ALLOC_SIZE 1024 |
| 65 | |
| 66 | static const struct tlv_definition ns_att_tlvdef = { |
| 67 | .def = { |
| 68 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 69 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 70 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 71 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 72 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 73 | }, |
| 74 | }; |
| 75 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 76 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 77 | static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 78 | uint16_t nsvci) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 79 | { |
| 80 | struct gprs_nsvc *nsvc; |
| 81 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 82 | if (nsvc->nsvci == nsvci) |
| 83 | return nsvc; |
| 84 | } |
| 85 | return NULL; |
| 86 | } |
| 87 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 88 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 89 | static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 90 | uint16_t nsei) |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 91 | { |
| 92 | struct gprs_nsvc *nsvc; |
| 93 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 94 | if (nsvc->nsei == nsei) |
| 95 | return nsvc; |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 101 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 102 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 103 | struct sockaddr_in *sin) |
| 104 | { |
| 105 | struct gprs_nsvc *nsvc; |
| 106 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 107 | if (!memcmp(&nsvc->ip.bts_addr, sin, sizeof(*sin))) |
| 108 | return nsvc; |
| 109 | } |
| 110 | return NULL; |
| 111 | } |
| 112 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 113 | static void gprs_ns_timer_cb(void *data); |
| 114 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 115 | 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] | 116 | { |
| 117 | struct gprs_nsvc *nsvc; |
| 118 | |
| 119 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 120 | nsvc->nsvci = nsvci; |
| 121 | /* before RESET procedure: BLOCKED and DEAD */ |
| 122 | nsvc->state = NSE_S_BLOCKED; |
| 123 | nsvc->nsi = nsi; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 124 | nsvc->timer.cb = gprs_ns_timer_cb; |
| 125 | nsvc->timer.data = nsvc; |
| 126 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 127 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 128 | |
| 129 | return nsvc; |
| 130 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 131 | |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 132 | static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal, |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 133 | uint8_t cause) |
| 134 | { |
| 135 | struct ns_signal_data nssd; |
| 136 | |
| 137 | nssd.nsvc = nsvc; |
| 138 | nssd.cause = cause; |
| 139 | |
| 140 | dispatch_signal(SS_NS, signal, &nssd); |
| 141 | } |
| 142 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 143 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 144 | static const struct value_string ns_cause_str[] = { |
| 145 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 146 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 147 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 148 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 149 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 150 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 151 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 152 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 153 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 154 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 155 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 156 | { 0, NULL } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 157 | }; |
| 158 | |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 159 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 160 | { |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 161 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 162 | } |
| 163 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 164 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 165 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 166 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 167 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 168 | int ret; |
| 169 | |
| 170 | switch (nsvc->nsi->ll) { |
| 171 | case GPRS_NS_LL_UDP: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 172 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 173 | break; |
| 174 | default: |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 175 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 176 | msgb_free(msg); |
| 177 | ret = -EIO; |
| 178 | break; |
| 179 | } |
| 180 | return ret; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 181 | } |
| 182 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 183 | 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] | 184 | { |
| 185 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 186 | struct gprs_ns_hdr *nsh; |
| 187 | |
| 188 | if (!msg) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 192 | |
| 193 | nsh->pdu_type = pdu_type; |
| 194 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 195 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 196 | } |
| 197 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 198 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 199 | { |
| 200 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 201 | struct gprs_ns_hdr *nsh; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 202 | uint16_t nsvci = htons(nsvc->nsvci); |
| 203 | uint16_t nsei = htons(nsvc->nsei); |
| 204 | |
| 205 | if (!msg) |
| 206 | return -ENOMEM; |
| 207 | |
| 208 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 209 | nsh->pdu_type = NS_PDUT_RESET; |
| 210 | |
| 211 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 212 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 213 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 214 | |
| 215 | return gprs_ns_tx(nsvc, msg); |
| 216 | |
| 217 | } |
| 218 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 219 | int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause, |
| 220 | uint16_t bvci, struct msgb *orig_msg) |
| 221 | { |
| 222 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 223 | struct gprs_ns_hdr *nsh; |
| 224 | uint16_t nsvci = htons(nsvc->nsvci); |
| 225 | |
| 226 | bvci = htons(bvci); |
| 227 | |
| 228 | if (!msg) |
| 229 | return -ENOMEM; |
| 230 | |
| 231 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 232 | nsh->pdu_type = NS_PDUT_STATUS; |
| 233 | |
| 234 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 235 | |
| 236 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 237 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 238 | cause == NS_CAUSE_NSVC_UNKNOWN) |
| 239 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 240 | |
| 241 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 242 | switch (cause) { |
| 243 | case NS_CAUSE_SEM_INCORR_PDU: |
| 244 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 245 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 246 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 247 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 248 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 249 | orig_msg->l2h); |
| 250 | break; |
| 251 | default: |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | /* Section 9.2.7.3: Static conditions for BVCI */ |
| 256 | if (cause == NS_CAUSE_BVCI_UNKNOWN) |
| 257 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci); |
| 258 | |
| 259 | return gprs_ns_tx(nsvc, msg); |
| 260 | } |
| 261 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 262 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause) |
| 263 | { |
| 264 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 265 | struct gprs_ns_hdr *nsh; |
| 266 | uint16_t nsvci = htons(nsvc->nsvci); |
| 267 | |
| 268 | if (!msg) |
| 269 | return -ENOMEM; |
| 270 | |
| 271 | /* be conservative and mark it as blocked even now! */ |
| 272 | nsvc->state |= NSE_S_BLOCKED; |
| 273 | |
| 274 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 275 | nsh->pdu_type = NS_PDUT_BLOCK; |
| 276 | |
| 277 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 278 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 279 | |
| 280 | return gprs_ns_tx(nsvc, msg); |
| 281 | } |
| 282 | |
| 283 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc) |
| 284 | { |
| 285 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 286 | } |
| 287 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 288 | #define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */ |
| 289 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 290 | static const uint8_t timer_mode_tout[_NSVC_TIMER_NR] = { |
| 291 | [NSVC_TIMER_TNS_RESET] = 60, |
| 292 | [NSVC_TIMER_TNS_ALIVE] = 3, |
| 293 | [NSVC_TIMER_TNS_TEST] = 30, |
| 294 | }; |
| 295 | |
| 296 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 297 | { |
| 298 | nsvc->alive_retries = 0; |
| 299 | |
| 300 | if (bsc_timer_pending(&nsvc->timer)) |
| 301 | bsc_del_timer(&nsvc->timer); |
| 302 | |
| 303 | nsvc->timer_mode = mode; |
| 304 | bsc_schedule_timer(&nsvc->timer, timer_mode_tout[mode], 0); |
| 305 | } |
| 306 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 307 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 308 | { |
| 309 | struct gprs_nsvc *nsvc = data; |
| 310 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 311 | switch (nsvc->timer_mode) { |
| 312 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 313 | /* Tns-alive case: we expired without response ! */ |
| 314 | nsvc->alive_retries++; |
| 315 | if (nsvc->alive_retries > NS_ALIVE_RETRIES) { |
| 316 | /* mark as dead and blocked */ |
| 317 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 318 | LOGP(DNS, LOGL_NOTICE, |
| 319 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 320 | "%u times, blocking NS-VC\n", nsvc->nsei, |
| 321 | NS_ALIVE_RETRIES); |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 322 | ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 323 | return; |
| 324 | } |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 325 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 326 | break; |
| 327 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 328 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 329 | gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 330 | /* start Tns-alive timer */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 331 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 332 | break; |
| 333 | case NSVC_TIMER_TNS_RESET: |
| 334 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 335 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 336 | /* Re-start Tns-reset timer */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 337 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 338 | break; |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 339 | case _NSVC_TIMER_NR: |
| 340 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 341 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Section 9.2.6 */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 345 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 346 | { |
| 347 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 348 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 349 | uint16_t nsvci, nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 350 | |
| 351 | if (!msg) |
| 352 | return -ENOMEM; |
| 353 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 354 | nsvci = htons(nsvc->nsvci); |
| 355 | nsei = htons(nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 356 | |
| 357 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 358 | |
| 359 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 360 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 361 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 362 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 363 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 364 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 365 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 366 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 367 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 368 | } |
| 369 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 370 | /* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */ |
| 371 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 372 | { |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 373 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 374 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 375 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 376 | |
| 377 | nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg)); |
| 378 | if (!nsvc) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 379 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 380 | "to NS-VC!\n", msgb_nsei(msg)); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 381 | return -EINVAL; |
| 382 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 383 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 384 | if (!(nsvc->state & NSE_S_ALIVE)) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 385 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 386 | nsvc->nsei); |
| 387 | return -EBUSY; |
| 388 | } |
| 389 | if (nsvc->state & NSE_S_BLOCKED) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 390 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 391 | nsvc->nsei); |
| 392 | return -EBUSY; |
| 393 | } |
| 394 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 395 | nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3); |
| 396 | if (!nsh) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 397 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 398 | return -EIO; |
| 399 | } |
| 400 | |
| 401 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 402 | /* spare octet in data[0] */ |
| 403 | nsh->data[1] = bvci >> 8; |
| 404 | nsh->data[2] = bvci & 0xff; |
| 405 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 406 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* Section 9.2.10: receive side */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 410 | 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] | 411 | { |
| 412 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 413 | uint16_t bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 414 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 415 | if (nsvc->state & NSE_S_BLOCKED) |
| 416 | return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED, |
| 417 | 0, msg); |
| 418 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 419 | /* spare octet in data[0] */ |
| 420 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 421 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 422 | msgb_bvci(msg) = bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 423 | |
| 424 | /* call upper layer (BSSGP) */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 425 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* Section 9.2.7 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 429 | 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] | 430 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 431 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 432 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 433 | uint8_t cause; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 434 | int rc; |
| 435 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 436 | LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 437 | |
| 438 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 439 | |
| 440 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 441 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 442 | return -EINVAL; |
| 443 | } |
| 444 | |
| 445 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 446 | LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* Section 7.3 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 452 | 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] | 453 | { |
| 454 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 455 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 456 | uint8_t *cause; |
| 457 | uint16_t *nsvci, *nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 458 | int rc; |
| 459 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 460 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 461 | |
| 462 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 463 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 464 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 465 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 466 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 467 | return -EINVAL; |
| 468 | } |
| 469 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 470 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 471 | nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 472 | nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 473 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 474 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 475 | nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause)); |
| 476 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 477 | /* Mark NS-VC as blocked and alive */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 478 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f44448d | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 479 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 480 | nsvc->nsei = ntohs(*nsei); |
| 481 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 482 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 483 | /* start the test procedure */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 484 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 485 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 486 | /* inform interested parties about the fact that this NSVC |
| 487 | * has received RESET */ |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 488 | ns_dispatch_signal(nsvc, S_NS_RESET, *cause); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 489 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 490 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 491 | } |
| 492 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 493 | static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 494 | { |
| 495 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 496 | struct tlv_parsed tp; |
| 497 | uint8_t *cause; |
| 498 | int rc; |
| 499 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 500 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 501 | |
| 502 | nsvc->state |= NSE_S_BLOCKED; |
| 503 | |
| 504 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 505 | |
| 506 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 507 | !TLVP_PRESENT(&tp, NS_IE_VCI)) { |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 508 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 509 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 514 | //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 515 | |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 516 | ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 517 | |
| 518 | return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK); |
| 519 | } |
| 520 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 521 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 522 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 523 | struct sockaddr_in *saddr) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 524 | { |
| 525 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 526 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 527 | int rc = 0; |
| 528 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 529 | /* look up the NSVC based on source address */ |
| 530 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 531 | if (!nsvc) { |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 532 | struct tlv_parsed tp; |
| 533 | uint16_t nsei; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 534 | /* Only the RESET procedure creates a new NSVC */ |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 535 | if (nsh->pdu_type != NS_PDUT_RESET) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 536 | LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x " |
Harald Welte | c8c3de3 | 2010-05-11 18:38:36 +0200 | [diff] [blame] | 537 | "from %s:%u for non-existing NS-VC\n", |
| 538 | nsh->pdu_type, inet_ntoa(saddr->sin_addr), |
| 539 | ntohs(saddr->sin_port)); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 540 | /* FIXME: send STATUS (but we have no NSVC!) */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 541 | //gprs_ns_tx_reset(nsvc, NS_CAUSE_NSVC_UNKNOWN); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 542 | return -EIO; |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 543 | } |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 544 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 545 | msgb_l2len(msg), 0, 0); |
| 546 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 547 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 548 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 549 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 550 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
| 551 | msg); |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 552 | return -EINVAL; |
| 553 | } |
| 554 | nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI)); |
| 555 | /* Check if we already know this NSEI, the remote end might |
| 556 | * simply have changed addresses, or it is a SGSN */ |
| 557 | nsvc = nsvc_by_nsei(nsi, nsei); |
| 558 | if (!nsvc) { |
| 559 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n", |
| 560 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
| 561 | nsvc = nsvc_create(nsi, 0xffff); |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 562 | } |
Harald Welte | 19b96b9 | 2010-05-11 18:40:45 +0200 | [diff] [blame] | 563 | /* Update the remote peer IP address/port */ |
| 564 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 565 | } else |
| 566 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 567 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 568 | switch (nsh->pdu_type) { |
| 569 | case NS_PDUT_ALIVE: |
| 570 | /* remote end inquires whether we're still alive, |
| 571 | * we need to respond with ALIVE_ACK */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 572 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 573 | break; |
| 574 | case NS_PDUT_ALIVE_ACK: |
| 575 | /* stop Tns-alive */ |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 576 | bsc_del_timer(&nsvc->timer); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 577 | /* start Tns-test */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 578 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 1bb9e79 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 579 | if (nsvc->remote_end_is_sgsn) { |
| 580 | /* FIXME: this should be one level higher */ |
| 581 | if (nsvc->state & NSE_S_BLOCKED) |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 582 | rc = gprs_ns_tx_unblock(nsvc); |
Harald Welte | 1bb9e79 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 583 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 584 | break; |
| 585 | case NS_PDUT_UNITDATA: |
| 586 | /* actual user data */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 587 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 588 | break; |
| 589 | case NS_PDUT_STATUS: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 590 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 591 | break; |
| 592 | case NS_PDUT_RESET: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 593 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 594 | break; |
| 595 | case NS_PDUT_RESET_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 596 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 597 | /* mark NS-VC as blocked + active */ |
| 598 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 599 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 600 | if (nsvc->remote_end_is_sgsn) { |
| 601 | /* stop RESET timer */ |
| 602 | bsc_del_timer(&nsvc->timer); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 603 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 604 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 605 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 606 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 607 | break; |
| 608 | case NS_PDUT_UNBLOCK: |
| 609 | /* Section 7.2: unblocking procedure */ |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 610 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 611 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 612 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 613 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 614 | break; |
| 615 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 616 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 617 | /* mark NS-VC as unblocked + active */ |
| 618 | nsvc->state = NSE_S_ALIVE; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 619 | nsvc->remote_state = NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 620 | break; |
| 621 | case NS_PDUT_BLOCK: |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 622 | rc = gprs_ns_rx_block(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 623 | break; |
| 624 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 625 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 626 | /* mark remote NS-VC as blocked + active */ |
| 627 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 628 | break; |
| 629 | default: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 630 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 631 | nsvc->nsei, nsh->pdu_type); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 632 | rc = -EINVAL; |
| 633 | break; |
| 634 | } |
| 635 | return rc; |
| 636 | } |
| 637 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 638 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb) |
| 639 | { |
| 640 | struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst); |
| 641 | |
| 642 | nsi->cb = cb; |
| 643 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
| 644 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 645 | return nsi; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 649 | { |
| 650 | /* FIXME: clear all timers */ |
| 651 | |
| 652 | /* recursively free the NSI and all its NSVCs */ |
| 653 | talloc_free(nsi); |
| 654 | } |
| 655 | |
| 656 | |
| 657 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 658 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 659 | |
| 660 | /* Read a single NS-over-IP message */ |
| 661 | static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error, |
| 662 | struct sockaddr_in *saddr) |
| 663 | { |
| 664 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS"); |
| 665 | int ret = 0; |
| 666 | socklen_t saddr_len = sizeof(*saddr); |
| 667 | |
| 668 | if (!msg) { |
| 669 | *error = -ENOMEM; |
| 670 | return NULL; |
| 671 | } |
| 672 | |
| 673 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 674 | (struct sockaddr *)saddr, &saddr_len); |
| 675 | if (ret < 0) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 676 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 677 | strerror(errno)); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 678 | msgb_free(msg); |
| 679 | *error = ret; |
| 680 | return NULL; |
| 681 | } else if (ret == 0) { |
| 682 | msgb_free(msg); |
| 683 | *error = ret; |
| 684 | return NULL; |
| 685 | } |
| 686 | |
| 687 | msg->l2h = msg->data; |
| 688 | msgb_put(msg, ret); |
| 689 | |
| 690 | return msg; |
| 691 | } |
| 692 | |
| 693 | static int handle_nsip_read(struct bsc_fd *bfd) |
| 694 | { |
| 695 | int error; |
| 696 | struct sockaddr_in saddr; |
| 697 | struct gprs_ns_inst *nsi = bfd->data; |
| 698 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 699 | |
| 700 | if (!msg) |
| 701 | return error; |
| 702 | |
| 703 | return gprs_ns_rcvmsg(nsi, msg, &saddr); |
| 704 | } |
| 705 | |
| 706 | static int handle_nsip_write(struct bsc_fd *bfd) |
| 707 | { |
| 708 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 709 | return -EIO; |
| 710 | } |
| 711 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 712 | int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 713 | { |
| 714 | int rc; |
| 715 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 716 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 717 | |
| 718 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 719 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 720 | |
| 721 | talloc_free(msg); |
| 722 | |
| 723 | return rc; |
| 724 | } |
| 725 | |
| 726 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
| 727 | static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 728 | { |
| 729 | int rc = 0; |
| 730 | |
| 731 | if (what & BSC_FD_READ) |
| 732 | rc = handle_nsip_read(bfd); |
| 733 | if (what & BSC_FD_WRITE) |
| 734 | rc = handle_nsip_write(bfd); |
| 735 | |
| 736 | return rc; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | /* FIXME: this is currently in input/ipaccess.c */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 741 | 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] | 742 | int (*cb)(struct bsc_fd *fd, unsigned int what)); |
| 743 | |
| 744 | /* Listen for incoming GPRS packets */ |
| 745 | int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port) |
| 746 | { |
| 747 | int ret; |
| 748 | |
| 749 | ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb); |
| 750 | if (ret < 0) |
| 751 | return ret; |
| 752 | |
| 753 | nsi->ll = GPRS_NS_LL_UDP; |
| 754 | nsi->nsip.fd.data = nsi; |
| 755 | |
| 756 | return ret; |
| 757 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 758 | |
| 759 | /* Establish a connection (from the BSS) to the SGSN */ |
| 760 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 761 | struct sockaddr_in *dest, uint16_t nsei, |
| 762 | uint16_t nsvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 763 | { |
| 764 | struct gprs_nsvc *nsvc; |
| 765 | |
| 766 | nsvc = nsvc_by_rem_addr(nsi, dest); |
| 767 | if (!nsvc) { |
| 768 | nsvc = nsvc_create(nsi, nsvci); |
| 769 | nsvc->ip.bts_addr = *dest; |
| 770 | } |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 771 | nsvc->nsei = nsei; |
| 772 | nsvc->nsvci = nsvci; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 773 | nsvc->remote_end_is_sgsn = 1; |
| 774 | |
| 775 | /* Initiate a RESET procedure */ |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 776 | /* Mark NS-VC locally as blocked and dead */ |
| 777 | nsvc->state = NSE_S_BLOCKED; |
| 778 | /* Send NS-RESET PDU */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 779 | if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 780 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 781 | nsei); |
| 782 | } |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame^] | 783 | /* Start Tns-reset */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 784 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 785 | |
| 786 | return nsvc; |
| 787 | } |