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 | |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 66 | /* FIXME: this should go to some common file as it is copied |
| 67 | * in vty_interface.c of the BSC */ |
| 68 | static const struct value_string gprs_ns_timer_strs[] = { |
| 69 | { 0, "tns-block" }, |
| 70 | { 1, "tns-block-retries" }, |
| 71 | { 2, "tns-reset" }, |
| 72 | { 3, "tns-reset-retries" }, |
| 73 | { 4, "tns-test" }, |
| 74 | { 5, "tns-alive" }, |
| 75 | { 6, "tns-alive-retries" }, |
| 76 | { 0, NULL } |
| 77 | }; |
| 78 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 79 | static const struct tlv_definition ns_att_tlvdef = { |
| 80 | .def = { |
| 81 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 82 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 83 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 84 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 85 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 86 | }, |
| 87 | }; |
| 88 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 89 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 90 | static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 91 | uint16_t nsvci) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 92 | { |
| 93 | struct gprs_nsvc *nsvc; |
| 94 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 95 | if (nsvc->nsvci == nsvci) |
| 96 | return nsvc; |
| 97 | } |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 101 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 102 | static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 103 | uint16_t nsei) |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 104 | { |
| 105 | struct gprs_nsvc *nsvc; |
| 106 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 107 | if (nsvc->nsei == nsei) |
| 108 | return nsvc; |
| 109 | } |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 114 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 115 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 116 | struct sockaddr_in *sin) |
| 117 | { |
| 118 | struct gprs_nsvc *nsvc; |
| 119 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
Harald Welte | 517b77f | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 120 | if (nsvc->ip.bts_addr.sin_addr.s_addr == |
| 121 | sin->sin_addr.s_addr && |
| 122 | nsvc->ip.bts_addr.sin_port == sin->sin_port) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 123 | return nsvc; |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 128 | static void gprs_ns_timer_cb(void *data); |
| 129 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 130 | 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] | 131 | { |
| 132 | struct gprs_nsvc *nsvc; |
| 133 | |
| 134 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 135 | nsvc->nsvci = nsvci; |
| 136 | /* before RESET procedure: BLOCKED and DEAD */ |
| 137 | nsvc->state = NSE_S_BLOCKED; |
| 138 | nsvc->nsi = nsi; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 139 | nsvc->timer.cb = gprs_ns_timer_cb; |
| 140 | nsvc->timer.data = nsvc; |
| 141 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 142 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 143 | |
| 144 | return nsvc; |
| 145 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 146 | |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 147 | static void nsvc_delete(struct gprs_nsvc *nsvc) |
| 148 | { |
| 149 | if (bsc_timer_pending(&nsvc->timer)) |
| 150 | bsc_del_timer(&nsvc->timer); |
| 151 | llist_del(&nsvc->list); |
| 152 | talloc_free(nsvc); |
| 153 | } |
| 154 | |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 155 | static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal, |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 156 | uint8_t cause) |
| 157 | { |
| 158 | struct ns_signal_data nssd; |
| 159 | |
| 160 | nssd.nsvc = nsvc; |
| 161 | nssd.cause = cause; |
| 162 | |
| 163 | dispatch_signal(SS_NS, signal, &nssd); |
| 164 | } |
| 165 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 166 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 167 | static const struct value_string ns_cause_str[] = { |
| 168 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 169 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 170 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 171 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 172 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 173 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 174 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 175 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 176 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 177 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 178 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 179 | { 0, NULL } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 180 | }; |
| 181 | |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 182 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 183 | { |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 184 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 185 | } |
| 186 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 187 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 188 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 189 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 190 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 191 | int ret; |
| 192 | |
| 193 | switch (nsvc->nsi->ll) { |
| 194 | case GPRS_NS_LL_UDP: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 195 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 196 | break; |
| 197 | default: |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 198 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 199 | msgb_free(msg); |
| 200 | ret = -EIO; |
| 201 | break; |
| 202 | } |
| 203 | return ret; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 204 | } |
| 205 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 206 | 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] | 207 | { |
| 208 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 209 | struct gprs_ns_hdr *nsh; |
| 210 | |
| 211 | if (!msg) |
| 212 | return -ENOMEM; |
| 213 | |
| 214 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 215 | |
| 216 | nsh->pdu_type = pdu_type; |
| 217 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 218 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 219 | } |
| 220 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 221 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 222 | { |
| 223 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 224 | struct gprs_ns_hdr *nsh; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 225 | uint16_t nsvci = htons(nsvc->nsvci); |
| 226 | uint16_t nsei = htons(nsvc->nsei); |
| 227 | |
| 228 | if (!msg) |
| 229 | return -ENOMEM; |
| 230 | |
Harald Welte | e21988c | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 231 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n", |
| 232 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 233 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 234 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 235 | nsh->pdu_type = NS_PDUT_RESET; |
| 236 | |
| 237 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 238 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 239 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 240 | |
| 241 | return gprs_ns_tx(nsvc, msg); |
| 242 | |
| 243 | } |
| 244 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 245 | int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause, |
| 246 | uint16_t bvci, struct msgb *orig_msg) |
| 247 | { |
| 248 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 249 | struct gprs_ns_hdr *nsh; |
| 250 | uint16_t nsvci = htons(nsvc->nsvci); |
| 251 | |
| 252 | bvci = htons(bvci); |
| 253 | |
| 254 | if (!msg) |
| 255 | return -ENOMEM; |
| 256 | |
Harald Welte | e21988c | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 257 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n", |
| 258 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 259 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 260 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 261 | nsh->pdu_type = NS_PDUT_STATUS; |
| 262 | |
| 263 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 264 | |
| 265 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 266 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 267 | cause == NS_CAUSE_NSVC_UNKNOWN) |
| 268 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 269 | |
| 270 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 271 | switch (cause) { |
| 272 | case NS_CAUSE_SEM_INCORR_PDU: |
| 273 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 274 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 275 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 276 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 277 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 278 | orig_msg->l2h); |
| 279 | break; |
| 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | /* Section 9.2.7.3: Static conditions for BVCI */ |
| 285 | if (cause == NS_CAUSE_BVCI_UNKNOWN) |
| 286 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci); |
| 287 | |
| 288 | return gprs_ns_tx(nsvc, msg); |
| 289 | } |
| 290 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 291 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause) |
| 292 | { |
| 293 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 294 | struct gprs_ns_hdr *nsh; |
| 295 | uint16_t nsvci = htons(nsvc->nsvci); |
| 296 | |
| 297 | if (!msg) |
| 298 | return -ENOMEM; |
| 299 | |
Harald Welte | e21988c | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 300 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n", |
| 301 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 302 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 303 | /* be conservative and mark it as blocked even now! */ |
| 304 | nsvc->state |= NSE_S_BLOCKED; |
| 305 | |
| 306 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 307 | nsh->pdu_type = NS_PDUT_BLOCK; |
| 308 | |
| 309 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 310 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 311 | |
| 312 | return gprs_ns_tx(nsvc, msg); |
| 313 | } |
| 314 | |
| 315 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc) |
| 316 | { |
Harald Welte | e21988c | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 317 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n", |
| 318 | nsvc->nsei, nsvc->nsvci); |
| 319 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 320 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 321 | } |
| 322 | |
Harald Welte | 9a4847f | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 323 | int gprs_ns_tx_alive(struct gprs_nsvc *nsvc) |
| 324 | { |
| 325 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n", |
| 326 | nsvc->nsei, nsvc->nsvci); |
| 327 | |
| 328 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 329 | } |
| 330 | |
| 331 | int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc) |
| 332 | { |
| 333 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n", |
| 334 | nsvc->nsei, nsvc->nsvci); |
| 335 | |
| 336 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
| 337 | } |
| 338 | |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 339 | static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = { |
| 340 | [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET, |
| 341 | [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE, |
| 342 | [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST, |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 343 | }; |
| 344 | |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 345 | static const struct value_string timer_mode_strs[] = { |
| 346 | { NSVC_TIMER_TNS_RESET, "tns-reset" }, |
| 347 | { NSVC_TIMER_TNS_ALIVE, "tns-alive" }, |
| 348 | { NSVC_TIMER_TNS_TEST, "tns-test" }, |
| 349 | { 0, NULL } |
| 350 | }; |
| 351 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 352 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 353 | { |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 354 | enum ns_timeout tout = timer_mode_tout[mode]; |
| 355 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
| 356 | |
Harald Welte | 4574420 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 357 | DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n", |
| 358 | nsvc->nsei, get_value_string(timer_mode_strs, mode), |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 359 | seconds); |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 360 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 361 | if (bsc_timer_pending(&nsvc->timer)) |
| 362 | bsc_del_timer(&nsvc->timer); |
| 363 | |
| 364 | nsvc->timer_mode = mode; |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 365 | bsc_schedule_timer(&nsvc->timer, seconds, 0); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 368 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 369 | { |
| 370 | struct gprs_nsvc *nsvc = data; |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 371 | enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode]; |
| 372 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 373 | |
Harald Welte | 4574420 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 374 | DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n", |
| 375 | nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode), |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 376 | seconds); |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 377 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 378 | switch (nsvc->timer_mode) { |
| 379 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 380 | /* Tns-alive case: we expired without response ! */ |
| 381 | nsvc->alive_retries++; |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 382 | if (nsvc->alive_retries > |
| 383 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 384 | /* mark as dead and blocked */ |
| 385 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 386 | LOGP(DNS, LOGL_NOTICE, |
| 387 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 388 | "%u times, blocking NS-VC\n", nsvc->nsei, |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 389 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]); |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 390 | ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0); |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 391 | ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 392 | return; |
| 393 | } |
Harald Welte | 9a4847f | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 394 | /* Tns-test case: send NS-ALIVE PDU */ |
| 395 | gprs_ns_tx_alive(nsvc); |
| 396 | /* start Tns-alive timer */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 397 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 398 | break; |
| 399 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 400 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | 9a4847f | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 401 | gprs_ns_tx_alive(nsvc); |
| 402 | /* start Tns-alive timer (transition into faster |
| 403 | * alive retransmissions) */ |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 404 | nsvc->alive_retries = 0; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 405 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 406 | break; |
| 407 | case NSVC_TIMER_TNS_RESET: |
| 408 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 409 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 410 | /* Re-start Tns-reset timer */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 411 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 412 | break; |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 413 | case _NSVC_TIMER_NR: |
| 414 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 415 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* Section 9.2.6 */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 419 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 420 | { |
| 421 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 422 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 423 | uint16_t nsvci, nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 424 | |
| 425 | if (!msg) |
| 426 | return -ENOMEM; |
| 427 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 428 | nsvci = htons(nsvc->nsvci); |
| 429 | nsei = htons(nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 430 | |
| 431 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 432 | |
| 433 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 434 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 435 | 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] | 436 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 437 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 438 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 439 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 440 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 441 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 442 | } |
| 443 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 444 | /* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */ |
| 445 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 446 | { |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 447 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 448 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 449 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 450 | |
| 451 | nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg)); |
| 452 | if (!nsvc) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 453 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 454 | "to NS-VC!\n", msgb_nsei(msg)); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 455 | return -EINVAL; |
| 456 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 457 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 458 | if (!(nsvc->state & NSE_S_ALIVE)) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 459 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 460 | nsvc->nsei); |
| 461 | return -EBUSY; |
| 462 | } |
| 463 | if (nsvc->state & NSE_S_BLOCKED) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 464 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 465 | nsvc->nsei); |
| 466 | return -EBUSY; |
| 467 | } |
| 468 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 469 | nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3); |
| 470 | if (!nsh) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 471 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 472 | return -EIO; |
| 473 | } |
| 474 | |
| 475 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 476 | /* spare octet in data[0] */ |
| 477 | nsh->data[1] = bvci >> 8; |
| 478 | nsh->data[2] = bvci & 0xff; |
| 479 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 480 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* Section 9.2.10: receive side */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 484 | 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] | 485 | { |
| 486 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 487 | uint16_t bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 488 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 489 | if (nsvc->state & NSE_S_BLOCKED) |
| 490 | return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED, |
| 491 | 0, msg); |
| 492 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 493 | /* spare octet in data[0] */ |
| 494 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 495 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 496 | msgb_bvci(msg) = bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 497 | |
| 498 | /* call upper layer (BSSGP) */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 499 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* Section 9.2.7 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 503 | 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] | 504 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 505 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 506 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 507 | uint8_t cause; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 508 | int rc; |
| 509 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 510 | LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 511 | |
| 512 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 513 | |
| 514 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 515 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 516 | return -EINVAL; |
| 517 | } |
| 518 | |
| 519 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 520 | LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | /* Section 7.3 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 526 | 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] | 527 | { |
| 528 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 529 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 530 | uint8_t *cause; |
| 531 | uint16_t *nsvci, *nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 532 | int rc; |
| 533 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 534 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 535 | |
| 536 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 537 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 538 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 539 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 540 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 541 | return -EINVAL; |
| 542 | } |
| 543 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 544 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 545 | nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 546 | nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 547 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 548 | 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] | 549 | nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause)); |
| 550 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 551 | /* Mark NS-VC as blocked and alive */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 552 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f44448d | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 553 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 554 | nsvc->nsei = ntohs(*nsei); |
| 555 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 556 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 557 | /* start the test procedure */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 558 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 559 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 560 | /* inform interested parties about the fact that this NSVC |
| 561 | * has received RESET */ |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 562 | ns_dispatch_signal(nsvc, S_NS_RESET, *cause); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 563 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 564 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 565 | } |
| 566 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 567 | static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 568 | { |
| 569 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 570 | struct tlv_parsed tp; |
| 571 | uint8_t *cause; |
| 572 | int rc; |
| 573 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 574 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 575 | |
| 576 | nsvc->state |= NSE_S_BLOCKED; |
| 577 | |
| 578 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 579 | |
| 580 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 581 | !TLVP_PRESENT(&tp, NS_IE_VCI)) { |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 582 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 583 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 584 | return -EINVAL; |
| 585 | } |
| 586 | |
| 587 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 588 | //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 589 | |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 590 | ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 591 | |
| 592 | return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK); |
| 593 | } |
| 594 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 595 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 596 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 597 | struct sockaddr_in *saddr) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 598 | { |
| 599 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 600 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 601 | int rc = 0; |
| 602 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 603 | /* look up the NSVC based on source address */ |
| 604 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 605 | if (!nsvc) { |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 606 | struct tlv_parsed tp; |
| 607 | uint16_t nsei; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 608 | /* Only the RESET procedure creates a new NSVC */ |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 609 | if (nsh->pdu_type != NS_PDUT_RESET) { |
Harald Welte | 78681af | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 610 | struct gprs_nsvc fake_nsvc; |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 611 | LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x " |
Harald Welte | c8c3de3 | 2010-05-11 18:38:36 +0200 | [diff] [blame] | 612 | "from %s:%u for non-existing NS-VC\n", |
| 613 | nsh->pdu_type, inet_ntoa(saddr->sin_addr), |
| 614 | ntohs(saddr->sin_port)); |
Harald Welte | 78681af | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 615 | /* Since we have no NSVC, we have to create a fake */ |
| 616 | fake_nsvc.nsvci = fake_nsvc.nsei = 0; |
| 617 | fake_nsvc.nsi = nsi; |
| 618 | fake_nsvc.ip.bts_addr = *saddr; |
| 619 | fake_nsvc.state = NSE_S_ALIVE; |
Harald Welte | 78681af | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 620 | return gprs_ns_tx_status(&fake_nsvc, |
| 621 | NS_CAUSE_PDU_INCOMP_PSTATE, 0, |
| 622 | msg); |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 623 | } |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 624 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 625 | msgb_l2len(msg), 0, 0); |
| 626 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 627 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 628 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 629 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 630 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
| 631 | msg); |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 632 | return -EINVAL; |
| 633 | } |
| 634 | nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI)); |
| 635 | /* Check if we already know this NSEI, the remote end might |
| 636 | * simply have changed addresses, or it is a SGSN */ |
| 637 | nsvc = nsvc_by_nsei(nsi, nsei); |
| 638 | if (!nsvc) { |
| 639 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n", |
| 640 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
| 641 | nsvc = nsvc_create(nsi, 0xffff); |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 642 | } |
Harald Welte | 19b96b9 | 2010-05-11 18:40:45 +0200 | [diff] [blame] | 643 | /* Update the remote peer IP address/port */ |
| 644 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 645 | } else |
| 646 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 647 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 648 | switch (nsh->pdu_type) { |
| 649 | case NS_PDUT_ALIVE: |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 650 | /* If we're dead and blocked and suddenly receive a |
| 651 | * NS-ALIVE out of the blue, we might have been re-started |
| 652 | * and should send a NS-RESET to make sure everything recovers |
| 653 | * fine. */ |
| 654 | if (nsvc->state == NSE_S_BLOCKED) |
| 655 | rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE); |
| 656 | else |
| 657 | rc = gprs_ns_tx_alive_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 658 | break; |
| 659 | case NS_PDUT_ALIVE_ACK: |
| 660 | /* stop Tns-alive */ |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 661 | bsc_del_timer(&nsvc->timer); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 662 | /* start Tns-test */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 663 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 1bb9e79 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 664 | if (nsvc->remote_end_is_sgsn) { |
| 665 | /* FIXME: this should be one level higher */ |
| 666 | if (nsvc->state & NSE_S_BLOCKED) |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 667 | rc = gprs_ns_tx_unblock(nsvc); |
Harald Welte | 1bb9e79 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 668 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 669 | break; |
| 670 | case NS_PDUT_UNITDATA: |
| 671 | /* actual user data */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 672 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 673 | break; |
| 674 | case NS_PDUT_STATUS: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 675 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 676 | break; |
| 677 | case NS_PDUT_RESET: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 678 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 679 | break; |
| 680 | case NS_PDUT_RESET_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 681 | 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] | 682 | /* mark NS-VC as blocked + active */ |
| 683 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 684 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 685 | if (nsvc->remote_end_is_sgsn) { |
| 686 | /* stop RESET timer */ |
| 687 | bsc_del_timer(&nsvc->timer); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 688 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 689 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 690 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 691 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 692 | break; |
| 693 | case NS_PDUT_UNBLOCK: |
| 694 | /* Section 7.2: unblocking procedure */ |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 695 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 696 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 697 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 698 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 699 | break; |
| 700 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 701 | 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] | 702 | /* mark NS-VC as unblocked + active */ |
| 703 | nsvc->state = NSE_S_ALIVE; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 704 | nsvc->remote_state = NSE_S_ALIVE; |
Harald Welte | 4108b38 | 2010-05-12 14:18:46 +0000 | [diff] [blame] | 705 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 706 | break; |
| 707 | case NS_PDUT_BLOCK: |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 708 | rc = gprs_ns_rx_block(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 709 | break; |
| 710 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 711 | 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] | 712 | /* mark remote NS-VC as blocked + active */ |
| 713 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 714 | break; |
| 715 | default: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 716 | 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] | 717 | nsvc->nsei, nsh->pdu_type); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 718 | rc = -EINVAL; |
| 719 | break; |
| 720 | } |
| 721 | return rc; |
| 722 | } |
| 723 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 724 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb) |
| 725 | { |
| 726 | struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst); |
| 727 | |
| 728 | nsi->cb = cb; |
| 729 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 730 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 731 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 732 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 733 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 734 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 735 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 736 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 737 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 738 | return nsi; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 742 | { |
| 743 | /* FIXME: clear all timers */ |
| 744 | |
| 745 | /* recursively free the NSI and all its NSVCs */ |
| 746 | talloc_free(nsi); |
| 747 | } |
| 748 | |
| 749 | |
| 750 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 751 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 752 | |
| 753 | /* Read a single NS-over-IP message */ |
| 754 | static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error, |
| 755 | struct sockaddr_in *saddr) |
| 756 | { |
| 757 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS"); |
| 758 | int ret = 0; |
| 759 | socklen_t saddr_len = sizeof(*saddr); |
| 760 | |
| 761 | if (!msg) { |
| 762 | *error = -ENOMEM; |
| 763 | return NULL; |
| 764 | } |
| 765 | |
| 766 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 767 | (struct sockaddr *)saddr, &saddr_len); |
| 768 | if (ret < 0) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 769 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 770 | strerror(errno)); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 771 | msgb_free(msg); |
| 772 | *error = ret; |
| 773 | return NULL; |
| 774 | } else if (ret == 0) { |
| 775 | msgb_free(msg); |
| 776 | *error = ret; |
| 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | msg->l2h = msg->data; |
| 781 | msgb_put(msg, ret); |
| 782 | |
| 783 | return msg; |
| 784 | } |
| 785 | |
| 786 | static int handle_nsip_read(struct bsc_fd *bfd) |
| 787 | { |
| 788 | int error; |
| 789 | struct sockaddr_in saddr; |
| 790 | struct gprs_ns_inst *nsi = bfd->data; |
| 791 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 792 | |
| 793 | if (!msg) |
| 794 | return error; |
| 795 | |
Harald Welte | d8f2271 | 2010-05-12 18:38:45 +0000 | [diff] [blame] | 796 | error = gprs_ns_rcvmsg(nsi, msg, &saddr); |
| 797 | |
| 798 | msgb_free(msg); |
| 799 | |
| 800 | return error; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | static int handle_nsip_write(struct bsc_fd *bfd) |
| 804 | { |
| 805 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 806 | return -EIO; |
| 807 | } |
| 808 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 809 | int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 810 | { |
| 811 | int rc; |
| 812 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 813 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 814 | |
| 815 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 816 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 817 | |
| 818 | talloc_free(msg); |
| 819 | |
| 820 | return rc; |
| 821 | } |
| 822 | |
| 823 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
| 824 | static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 825 | { |
| 826 | int rc = 0; |
| 827 | |
| 828 | if (what & BSC_FD_READ) |
| 829 | rc = handle_nsip_read(bfd); |
| 830 | if (what & BSC_FD_WRITE) |
| 831 | rc = handle_nsip_write(bfd); |
| 832 | |
| 833 | return rc; |
| 834 | } |
| 835 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 836 | 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] | 837 | int (*cb)(struct bsc_fd *fd, unsigned int what)); |
| 838 | |
| 839 | /* Listen for incoming GPRS packets */ |
| 840 | int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port) |
| 841 | { |
| 842 | int ret; |
| 843 | |
| 844 | ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb); |
| 845 | if (ret < 0) |
| 846 | return ret; |
| 847 | |
| 848 | nsi->ll = GPRS_NS_LL_UDP; |
| 849 | nsi->nsip.fd.data = nsi; |
| 850 | |
| 851 | return ret; |
| 852 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 853 | |
| 854 | /* Establish a connection (from the BSS) to the SGSN */ |
| 855 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 856 | struct sockaddr_in *dest, uint16_t nsei, |
| 857 | uint16_t nsvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 858 | { |
| 859 | struct gprs_nsvc *nsvc; |
| 860 | |
| 861 | nsvc = nsvc_by_rem_addr(nsi, dest); |
Harald Welte | 517b77f | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 862 | if (!nsvc) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 863 | nsvc = nsvc_create(nsi, nsvci); |
Harald Welte | 517b77f | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 864 | nsvc->ip.bts_addr = *dest; |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 865 | nsvc->nsei = nsei; |
| 866 | nsvc->nsvci = nsvci; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 867 | nsvc->remote_end_is_sgsn = 1; |
| 868 | |
| 869 | /* Initiate a RESET procedure */ |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 870 | /* Mark NS-VC locally as blocked and dead */ |
| 871 | nsvc->state = NSE_S_BLOCKED; |
| 872 | /* Send NS-RESET PDU */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 873 | if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 874 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 875 | nsei); |
| 876 | } |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 877 | /* Start Tns-reset */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 878 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 879 | |
| 880 | return nsvc; |
| 881 | } |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 882 | |
| 883 | #include <vty/vty.h> |
| 884 | #include <vty/command.h> |
| 885 | |
| 886 | static struct gprs_ns_inst *vty_nsi = NULL; |
| 887 | |
| 888 | static struct cmd_node ns_node = { |
| 889 | NS_NODE, |
| 890 | "%s(ns)#", |
| 891 | 1, |
| 892 | }; |
| 893 | |
| 894 | static int config_write_ns(struct vty *vty) |
| 895 | { |
| 896 | struct gprs_nsvc *nsvc; |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 897 | unsigned int i; |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 898 | |
| 899 | vty_out(vty, "ns%s", VTY_NEWLINE); |
| 900 | |
| 901 | llist_for_each_entry(nsvc, &vty_nsi->gprs_nsvcs, list) { |
| 902 | if (!nsvc->persistent) |
| 903 | continue; |
| 904 | vty_out(vty, " nse %u nsvci %u%s", |
| 905 | nsvc->nsei, nsvc->nsvci, VTY_NEWLINE); |
| 906 | vty_out(vty, " nse %u remote-role %s%s", |
| 907 | nsvc->nsei, nsvc->remote_end_is_sgsn ? "sgsn" : "bss", |
| 908 | VTY_NEWLINE); |
| 909 | if (nsvc->nsi->ll == GPRS_NS_LL_UDP) { |
| 910 | vty_out(vty, " nse %u remote-ip %s%s", |
| 911 | nsvc->nsei, |
| 912 | inet_ntoa(nsvc->ip.bts_addr.sin_addr), |
| 913 | VTY_NEWLINE); |
| 914 | vty_out(vty, " nse %u remote-port %u%s", |
| 915 | nsvc->nsei, ntohs(nsvc->ip.bts_addr.sin_port), |
| 916 | VTY_NEWLINE); |
| 917 | } |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 920 | for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++) |
| 921 | vty_out(vty, " timer %s %u%s", |
| 922 | get_value_string(gprs_ns_timer_strs, i), |
| 923 | vty_nsi->timeout[i], VTY_NEWLINE); |
| 924 | |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 925 | return CMD_SUCCESS; |
| 926 | } |
| 927 | |
| 928 | DEFUN(cfg_ns, cfg_ns_cmd, |
| 929 | "ns", |
| 930 | "Configure the GPRS Network Service") |
| 931 | { |
| 932 | vty->node = NS_NODE; |
| 933 | return CMD_SUCCESS; |
| 934 | } |
| 935 | |
| 936 | DEFUN(show_ns, show_ns_cmd, "show ns", |
| 937 | SHOW_STR "Display information about the NS protocol") |
| 938 | { |
| 939 | struct gprs_ns_inst *nsi = vty_nsi; |
| 940 | struct gprs_nsvc *nsvc; |
| 941 | |
| 942 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 943 | vty_out(vty, "NSEI %5u, NS-VC %5u, Remote: %-4s, %5s %9s", |
| 944 | nsvc->nsei, nsvc->nsvci, |
| 945 | nsvc->remote_end_is_sgsn ? "SGSN" : "BSS", |
| 946 | nsvc->state & NSE_S_ALIVE ? "ALIVE" : "DEAD", |
| 947 | nsvc->state & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED"); |
| 948 | if (nsvc->nsi->ll == GPRS_NS_LL_UDP) |
| 949 | vty_out(vty, ", %15s:%u", |
| 950 | inet_ntoa(nsvc->ip.bts_addr.sin_addr), |
| 951 | ntohs(nsvc->ip.bts_addr.sin_port)); |
| 952 | vty_out(vty, "%s", VTY_NEWLINE); |
| 953 | } |
| 954 | |
| 955 | return CMD_SUCCESS; |
| 956 | } |
| 957 | |
| 958 | |
| 959 | #define NSE_CMD_STR "NS Entity\n" "NS Entity ID (NSEI)\n" |
| 960 | |
| 961 | DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd, |
| 962 | "nse <0-65535> nsvci <0-65534>", |
| 963 | NSE_CMD_STR |
| 964 | "NS Virtual Connection\n" |
| 965 | "NS Virtual Connection ID (NSVCI)\n" |
| 966 | ) |
| 967 | { |
| 968 | uint16_t nsei = atoi(argv[0]); |
| 969 | uint16_t nsvci = atoi(argv[1]); |
| 970 | struct gprs_nsvc *nsvc; |
| 971 | |
| 972 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 973 | if (!nsvc) { |
| 974 | nsvc = nsvc_create(vty_nsi, nsvci); |
| 975 | nsvc->nsei = nsei; |
| 976 | } |
| 977 | nsvc->nsvci = nsvci; |
| 978 | /* All NSVCs that are explicitly configured by VTY are |
| 979 | * marked as persistent so we can write them to the config |
| 980 | * file at some later point */ |
| 981 | nsvc->persistent = 1; |
| 982 | |
| 983 | return CMD_SUCCESS; |
| 984 | } |
| 985 | |
| 986 | DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd, |
| 987 | "nse <0-65535> remote-ip A.B.C.D", |
| 988 | NSE_CMD_STR |
| 989 | "Remote IP Address\n" |
| 990 | "Remote IP Address\n") |
| 991 | { |
| 992 | uint16_t nsei = atoi(argv[0]); |
| 993 | struct gprs_nsvc *nsvc; |
| 994 | |
| 995 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 996 | if (!nsvc) { |
| 997 | vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE); |
| 998 | return CMD_WARNING; |
| 999 | } |
| 1000 | inet_aton(argv[1], &nsvc->ip.bts_addr.sin_addr); |
| 1001 | |
| 1002 | return CMD_SUCCESS; |
| 1003 | |
| 1004 | } |
| 1005 | |
| 1006 | DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd, |
| 1007 | "nse <0-65535> remote-port <0-65535>", |
| 1008 | NSE_CMD_STR |
| 1009 | "Remote UDP Port\n" |
| 1010 | "Remote UDP Port Number\n") |
| 1011 | { |
| 1012 | uint16_t nsei = atoi(argv[0]); |
| 1013 | uint16_t port = atoi(argv[1]); |
| 1014 | struct gprs_nsvc *nsvc; |
| 1015 | |
| 1016 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 1017 | if (!nsvc) { |
| 1018 | vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE); |
| 1019 | return CMD_WARNING; |
| 1020 | } |
| 1021 | |
| 1022 | nsvc->ip.bts_addr.sin_port = htons(port); |
| 1023 | |
| 1024 | return CMD_SUCCESS; |
| 1025 | } |
| 1026 | |
| 1027 | DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd, |
| 1028 | "nse <0-65535> remote-role (sgsn|bss)", |
| 1029 | NSE_CMD_STR |
| 1030 | "Remote NSE Role\n" |
| 1031 | "Remote Peer is SGSN\n" |
| 1032 | "Remote Peer is BSS\n") |
| 1033 | { |
| 1034 | uint16_t nsei = atoi(argv[0]); |
| 1035 | struct gprs_nsvc *nsvc; |
| 1036 | |
| 1037 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 1038 | if (!nsvc) { |
| 1039 | vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE); |
| 1040 | return CMD_WARNING; |
| 1041 | } |
| 1042 | |
| 1043 | if (!strcmp(argv[1], "sgsn")) |
| 1044 | nsvc->remote_end_is_sgsn = 1; |
| 1045 | else |
| 1046 | nsvc->remote_end_is_sgsn = 0; |
| 1047 | |
| 1048 | return CMD_SUCCESS; |
| 1049 | } |
| 1050 | |
| 1051 | DEFUN(cfg_no_nse, cfg_no_nse_cmd, |
| 1052 | "no nse <0-65535>", |
| 1053 | "Delete NS Entity\n" |
| 1054 | "Delete " NSE_CMD_STR) |
| 1055 | { |
| 1056 | uint16_t nsei = atoi(argv[0]); |
| 1057 | struct gprs_nsvc *nsvc; |
| 1058 | |
| 1059 | nsvc = nsvc_by_nsei(vty_nsi, nsei); |
| 1060 | if (!nsvc) { |
| 1061 | vty_out(vty, "No such NSE (%u)%s", nsei, VTY_NEWLINE); |
| 1062 | return CMD_WARNING; |
| 1063 | } |
| 1064 | |
| 1065 | nsvc_delete(nsvc); |
| 1066 | |
| 1067 | return CMD_SUCCESS; |
| 1068 | } |
| 1069 | |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 1070 | DEFUN(cfg_ns_timer, cfg_ns_timer_cmd, |
| 1071 | "timer " NS_TIMERS " <0-65535>", |
| 1072 | "Network Service Timer\n" |
| 1073 | NS_TIMERS_HELP "Timer Value\n") |
| 1074 | { |
| 1075 | int idx = get_string_value(gprs_ns_timer_strs, argv[0]); |
| 1076 | int val = atoi(argv[1]); |
| 1077 | |
| 1078 | if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout)) |
| 1079 | return CMD_WARNING; |
| 1080 | |
| 1081 | vty_nsi->timeout[idx] = val; |
| 1082 | |
| 1083 | return CMD_SUCCESS; |
| 1084 | } |
| 1085 | |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 1086 | int gprs_ns_vty_init(struct gprs_ns_inst *nsi) |
| 1087 | { |
| 1088 | vty_nsi = nsi; |
| 1089 | |
Harald Welte | c91af6a | 2010-05-12 16:50:52 +0000 | [diff] [blame] | 1090 | install_element_ve(&show_ns_cmd); |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 1091 | |
| 1092 | install_element(CONFIG_NODE, &cfg_ns_cmd); |
| 1093 | install_node(&ns_node, config_write_ns); |
| 1094 | install_default(NS_NODE); |
| 1095 | install_element(NS_NODE, &cfg_nse_nsvci_cmd); |
| 1096 | install_element(NS_NODE, &cfg_nse_remoteip_cmd); |
| 1097 | install_element(NS_NODE, &cfg_nse_remoteport_cmd); |
| 1098 | install_element(NS_NODE, &cfg_nse_remoterole_cmd); |
| 1099 | install_element(NS_NODE, &cfg_no_nse_cmd); |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 1100 | install_element(NS_NODE, &cfg_ns_timer_cmd); |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 1101 | |
| 1102 | return 0; |
| 1103 | } |