Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1 | /* GPRS Networks Service (NS) messages on the Gb interfacebvci = msgb_bvci(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) */ |
| 3 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 4 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | /* Some introduction into NS: NS is used typically on top of frame relay, |
| 25 | * but in the ip.access world it is encapsulated in UDP packets. It serves |
| 26 | * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't |
| 27 | * do much, apart from providing congestion notification and status indication. |
| 28 | * |
| 29 | * Terms: |
| 30 | * NS Network Service |
| 31 | * NSVC NS Virtual Connection |
| 32 | * NSEI NS Entity Identifier |
| 33 | * NSVL NS Virtual Link |
| 34 | * NSVLI NS Virtual Link Identifier |
| 35 | * BVC BSSGP Virtual Connection |
| 36 | * BVCI BSSGP Virtual Connection Identifier |
| 37 | * NSVCG NS Virtual Connection Goup |
| 38 | * Blocked NS-VC cannot be used for user traffic |
| 39 | * Alive Ability of a NS-VC to provide communication |
| 40 | * |
| 41 | * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will |
| 42 | * therefore identify the BSSGP virtual connection by a BVCI passed down to NS. |
| 43 | * NS then has to firgure out which NSVC's are responsible for this BVCI. |
| 44 | * Those mappings are administratively configured. |
| 45 | */ |
| 46 | |
Harald Welte | 9aa97fc | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 47 | /* This implementation has the following limitations: |
| 48 | * o Only one NS-VC for each NSE: No load-sharing function |
| 49 | * o NSVCI 65535 and 65534 are reserved for internal use |
| 50 | * o Only UDP is supported as of now, no frame relay support |
| 51 | * o The IP Sub-Network-Service (SNS) as specified in 48.016 is not implemented |
| 52 | * o There are no BLOCK and UNBLOCK timers (yet?) |
| 53 | */ |
| 54 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 55 | #include <stdlib.h> |
| 56 | #include <unistd.h> |
| 57 | #include <errno.h> |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 58 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 59 | |
| 60 | #include <arpa/inet.h> |
| 61 | |
| 62 | #include <openbsc/gsm_data.h> |
| 63 | #include <osmocore/msgb.h> |
| 64 | #include <osmocore/tlv.h> |
| 65 | #include <osmocore/talloc.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 66 | #include <osmocore/select.h> |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 67 | #include <osmocore/rate_ctr.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 68 | #include <openbsc/debug.h> |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 69 | #include <openbsc/signal.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 70 | #include <openbsc/gprs_ns.h> |
| 71 | #include <openbsc/gprs_bssgp.h> |
| 72 | |
| 73 | #define NS_ALLOC_SIZE 1024 |
| 74 | |
| 75 | static const struct tlv_definition ns_att_tlvdef = { |
| 76 | .def = { |
| 77 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 78 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 79 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 80 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 81 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 82 | }, |
| 83 | }; |
| 84 | |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 85 | enum ns_ctr { |
| 86 | NS_CTR_PKTS_IN, |
| 87 | NS_CTR_PKTS_OUT, |
| 88 | NS_CTR_BYTES_IN, |
| 89 | NS_CTR_BYTES_OUT, |
| 90 | NS_CTR_BLOCKED, |
| 91 | NS_CTR_DEAD, |
| 92 | }; |
| 93 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 94 | static const struct rate_ctr_desc nsvc_ctr_description[] = { |
| 95 | { "packets.in", "Packets at NS Level ( In)" }, |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 96 | { "packets.out","Packets at NS Level (Out)" }, |
| 97 | { "bytes.in", "Bytes at NS Level ( In)" }, |
| 98 | { "bytes.out", "Bytes at NS Level (Out)" }, |
| 99 | { "blocked", "NS-VC Block count " }, |
| 100 | { "dead", "NS-VC gone dead count " }, |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static const struct rate_ctr_group_desc nsvc_ctrg_desc = { |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 104 | .group_name_prefix = "ns.nsvc", |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 105 | .group_description = "NSVC Peer Statistics", |
| 106 | .num_ctr = ARRAY_SIZE(nsvc_ctr_description), |
| 107 | .ctr_desc = nsvc_ctr_description, |
| 108 | }; |
| 109 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 110 | /* Lookup struct gprs_nsvc based on NSVCI */ |
Harald Welte | ff56d61 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 111 | struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci) |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 112 | { |
| 113 | struct gprs_nsvc *nsvc; |
| 114 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 115 | if (nsvc->nsvci == nsvci) |
| 116 | return nsvc; |
| 117 | } |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 121 | /* Lookup struct gprs_nsvc based on NSVCI */ |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 122 | struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei) |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 123 | { |
| 124 | struct gprs_nsvc *nsvc; |
| 125 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 126 | if (nsvc->nsei == nsei) |
| 127 | return nsvc; |
| 128 | } |
| 129 | return NULL; |
| 130 | } |
| 131 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 132 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 133 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 134 | struct sockaddr_in *sin) |
| 135 | { |
| 136 | struct gprs_nsvc *nsvc; |
| 137 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
Harald Welte | cad8301 | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 138 | if (nsvc->ip.bts_addr.sin_addr.s_addr == |
| 139 | sin->sin_addr.s_addr && |
| 140 | nsvc->ip.bts_addr.sin_port == sin->sin_port) |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 141 | return nsvc; |
| 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 146 | static void gprs_ns_timer_cb(void *data); |
| 147 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 148 | struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci) |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 149 | { |
| 150 | struct gprs_nsvc *nsvc; |
| 151 | |
| 152 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 153 | nsvc->nsvci = nsvci; |
| 154 | /* before RESET procedure: BLOCKED and DEAD */ |
| 155 | nsvc->state = NSE_S_BLOCKED; |
| 156 | nsvc->nsi = nsi; |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 157 | nsvc->timer.cb = gprs_ns_timer_cb; |
| 158 | nsvc->timer.data = nsvc; |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 159 | nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci); |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 160 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 161 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 162 | |
| 163 | return nsvc; |
| 164 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 165 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 166 | void nsvc_delete(struct gprs_nsvc *nsvc) |
Harald Welte | 1194b58 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 167 | { |
| 168 | if (bsc_timer_pending(&nsvc->timer)) |
| 169 | bsc_del_timer(&nsvc->timer); |
| 170 | llist_del(&nsvc->list); |
| 171 | talloc_free(nsvc); |
| 172 | } |
| 173 | |
Harald Welte | 1389ac7 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 174 | static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal, |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 175 | uint8_t cause) |
| 176 | { |
| 177 | struct ns_signal_data nssd; |
| 178 | |
| 179 | nssd.nsvc = nsvc; |
| 180 | nssd.cause = cause; |
| 181 | |
| 182 | dispatch_signal(SS_NS, signal, &nssd); |
| 183 | } |
| 184 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 185 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 345223e | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 186 | static const struct value_string ns_cause_str[] = { |
| 187 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 188 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 189 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 190 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 191 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 192 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 193 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 194 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 195 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 345223e | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 196 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 197 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 198 | { 0, NULL } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 199 | }; |
| 200 | |
Harald Welte | 345223e | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 201 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 202 | { |
Harald Welte | 345223e | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 203 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 204 | } |
| 205 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 206 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 207 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 208 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 209 | { |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 210 | int ret; |
| 211 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 212 | log_set_context(BSC_CTX_NSVC, nsvc); |
| 213 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 214 | /* Increment number of Uplink bytes */ |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 215 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]); |
| 216 | rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg)); |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 217 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 218 | switch (nsvc->nsi->ll) { |
| 219 | case GPRS_NS_LL_UDP: |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 220 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 221 | break; |
| 222 | default: |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 223 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 224 | msgb_free(msg); |
| 225 | ret = -EIO; |
| 226 | break; |
| 227 | } |
| 228 | return ret; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 231 | static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 232 | { |
| 233 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 234 | struct gprs_ns_hdr *nsh; |
| 235 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 236 | log_set_context(BSC_CTX_NSVC, nsvc); |
| 237 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 238 | if (!msg) |
| 239 | return -ENOMEM; |
| 240 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 241 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 242 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 243 | |
| 244 | nsh->pdu_type = pdu_type; |
| 245 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 246 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 249 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 250 | { |
| 251 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 252 | struct gprs_ns_hdr *nsh; |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 253 | uint16_t nsvci = htons(nsvc->nsvci); |
| 254 | uint16_t nsei = htons(nsvc->nsei); |
| 255 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 256 | log_set_context(BSC_CTX_NSVC, nsvc); |
| 257 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 258 | if (!msg) |
| 259 | return -ENOMEM; |
| 260 | |
Harald Welte | 34caeb3 | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 261 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n", |
| 262 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 263 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 264 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 265 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 266 | nsh->pdu_type = NS_PDUT_RESET; |
| 267 | |
| 268 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 269 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 270 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 271 | |
| 272 | return gprs_ns_tx(nsvc, msg); |
| 273 | |
| 274 | } |
| 275 | |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 276 | int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause, |
| 277 | uint16_t bvci, struct msgb *orig_msg) |
| 278 | { |
| 279 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 280 | struct gprs_ns_hdr *nsh; |
| 281 | uint16_t nsvci = htons(nsvc->nsvci); |
| 282 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 283 | log_set_context(BSC_CTX_NSVC, nsvc); |
| 284 | |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 285 | bvci = htons(bvci); |
| 286 | |
| 287 | if (!msg) |
| 288 | return -ENOMEM; |
| 289 | |
Harald Welte | ff56d61 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 290 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n", |
Harald Welte | 34caeb3 | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 291 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 292 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 293 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 294 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 295 | nsh->pdu_type = NS_PDUT_STATUS; |
| 296 | |
| 297 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 298 | |
| 299 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 300 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 301 | cause == NS_CAUSE_NSVC_UNKNOWN) |
| 302 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 303 | |
| 304 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 305 | switch (cause) { |
| 306 | case NS_CAUSE_SEM_INCORR_PDU: |
| 307 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 308 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 309 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 310 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 311 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 312 | orig_msg->l2h); |
| 313 | break; |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | /* Section 9.2.7.3: Static conditions for BVCI */ |
| 319 | if (cause == NS_CAUSE_BVCI_UNKNOWN) |
| 320 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci); |
| 321 | |
| 322 | return gprs_ns_tx(nsvc, msg); |
| 323 | } |
| 324 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 325 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause) |
| 326 | { |
| 327 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 328 | struct gprs_ns_hdr *nsh; |
| 329 | uint16_t nsvci = htons(nsvc->nsvci); |
| 330 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 331 | log_set_context(BSC_CTX_NSVC, nsvc); |
| 332 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 333 | if (!msg) |
| 334 | return -ENOMEM; |
| 335 | |
Harald Welte | 34caeb3 | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 336 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n", |
| 337 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 338 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 339 | /* be conservative and mark it as blocked even now! */ |
| 340 | nsvc->state |= NSE_S_BLOCKED; |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 341 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 342 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 343 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 344 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 345 | nsh->pdu_type = NS_PDUT_BLOCK; |
| 346 | |
| 347 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 348 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 349 | |
| 350 | return gprs_ns_tx(nsvc, msg); |
| 351 | } |
| 352 | |
| 353 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc) |
| 354 | { |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 355 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | 34caeb3 | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 356 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n", |
| 357 | nsvc->nsei, nsvc->nsvci); |
| 358 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 359 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 360 | } |
| 361 | |
Harald Welte | bca900d | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 362 | int gprs_ns_tx_alive(struct gprs_nsvc *nsvc) |
| 363 | { |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 364 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | bca900d | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 365 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n", |
| 366 | nsvc->nsei, nsvc->nsvci); |
| 367 | |
| 368 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 369 | } |
| 370 | |
| 371 | int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc) |
| 372 | { |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 373 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | bca900d | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 374 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n", |
| 375 | nsvc->nsei, nsvc->nsvci); |
| 376 | |
| 377 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
| 378 | } |
| 379 | |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 380 | static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = { |
| 381 | [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET, |
| 382 | [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE, |
| 383 | [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST, |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 384 | }; |
| 385 | |
Harald Welte | 811c497 | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 386 | static const struct value_string timer_mode_strs[] = { |
| 387 | { NSVC_TIMER_TNS_RESET, "tns-reset" }, |
| 388 | { NSVC_TIMER_TNS_ALIVE, "tns-alive" }, |
| 389 | { NSVC_TIMER_TNS_TEST, "tns-test" }, |
| 390 | { 0, NULL } |
| 391 | }; |
| 392 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 393 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 394 | { |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 395 | enum ns_timeout tout = timer_mode_tout[mode]; |
| 396 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
| 397 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 398 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | d4eaf80 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 399 | DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n", |
| 400 | nsvc->nsei, get_value_string(timer_mode_strs, mode), |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 401 | seconds); |
Harald Welte | 811c497 | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 402 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 403 | if (bsc_timer_pending(&nsvc->timer)) |
| 404 | bsc_del_timer(&nsvc->timer); |
| 405 | |
| 406 | nsvc->timer_mode = mode; |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 407 | bsc_schedule_timer(&nsvc->timer, seconds, 0); |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 410 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 411 | { |
| 412 | struct gprs_nsvc *nsvc = data; |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 413 | enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode]; |
| 414 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 415 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 416 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | d4eaf80 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 417 | DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n", |
| 418 | nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode), |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 419 | seconds); |
Harald Welte | 811c497 | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 420 | |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 421 | switch (nsvc->timer_mode) { |
| 422 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 423 | /* Tns-alive case: we expired without response ! */ |
| 424 | nsvc->alive_retries++; |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 425 | if (nsvc->alive_retries > |
| 426 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 427 | /* mark as dead and blocked */ |
| 428 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 429 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
| 430 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]); |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 431 | LOGP(DNS, LOGL_NOTICE, |
| 432 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 433 | "%u times, blocking NS-VC\n", nsvc->nsei, |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 434 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]); |
Harald Welte | b778d2c | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 435 | ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0); |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 436 | ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 437 | return; |
| 438 | } |
Harald Welte | bca900d | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 439 | /* Tns-test case: send NS-ALIVE PDU */ |
| 440 | gprs_ns_tx_alive(nsvc); |
| 441 | /* start Tns-alive timer */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 442 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 443 | break; |
| 444 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 445 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | bca900d | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 446 | gprs_ns_tx_alive(nsvc); |
| 447 | /* start Tns-alive timer (transition into faster |
| 448 | * alive retransmissions) */ |
Harald Welte | 811c497 | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 449 | nsvc->alive_retries = 0; |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 450 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 451 | break; |
| 452 | case NSVC_TIMER_TNS_RESET: |
| 453 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 90de93e | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 454 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 455 | /* Re-start Tns-reset timer */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 456 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 05b320a | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 457 | break; |
Harald Welte | 1389ac7 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 458 | case _NSVC_TIMER_NR: |
| 459 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 460 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* Section 9.2.6 */ |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 464 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 465 | { |
| 466 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 467 | struct gprs_ns_hdr *nsh; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 468 | uint16_t nsvci, nsei; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 469 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 470 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 471 | if (!msg) |
| 472 | return -ENOMEM; |
| 473 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 474 | nsvci = htons(nsvc->nsvci); |
| 475 | nsei = htons(nsvc->nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 476 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 477 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 478 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 479 | |
| 480 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 481 | |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 482 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n", |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 483 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 484 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 485 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 486 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 487 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 488 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 489 | } |
| 490 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 491 | /* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */ |
| 492 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 493 | { |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 494 | struct gprs_nsvc *nsvc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 495 | struct gprs_ns_hdr *nsh; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 496 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 497 | |
| 498 | nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg)); |
| 499 | if (!nsvc) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 500 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 501 | "to NS-VC!\n", msgb_nsei(msg)); |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 502 | return -EINVAL; |
| 503 | } |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 504 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 505 | |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 506 | if (!(nsvc->state & NSE_S_ALIVE)) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 507 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n", |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 508 | nsvc->nsei); |
| 509 | return -EBUSY; |
| 510 | } |
| 511 | if (nsvc->state & NSE_S_BLOCKED) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 512 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n", |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 513 | nsvc->nsei); |
| 514 | return -EBUSY; |
| 515 | } |
| 516 | |
Harald Welte | e183345 | 2010-05-13 12:18:49 +0200 | [diff] [blame] | 517 | msg->l2h = msgb_push(msg, sizeof(*nsh) + 3); |
| 518 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 519 | if (!nsh) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 520 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 521 | return -EIO; |
| 522 | } |
| 523 | |
| 524 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 525 | /* spare octet in data[0] */ |
| 526 | nsh->data[1] = bvci >> 8; |
| 527 | nsh->data[2] = bvci & 0xff; |
| 528 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 529 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* Section 9.2.10: receive side */ |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 533 | static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 534 | { |
| 535 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 536 | uint16_t bvci; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 537 | |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 538 | if (nsvc->state & NSE_S_BLOCKED) |
| 539 | return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED, |
| 540 | 0, msg); |
| 541 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 542 | /* spare octet in data[0] */ |
| 543 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | fd3fa1d | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 544 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 545 | msgb_bvci(msg) = bvci; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 546 | |
| 547 | /* call upper layer (BSSGP) */ |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 548 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* Section 9.2.7 */ |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 552 | static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 553 | { |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 554 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 555 | struct tlv_parsed tp; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 556 | uint8_t cause; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 557 | int rc; |
| 558 | |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 559 | LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 560 | |
| 561 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 562 | |
| 563 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 564 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 565 | return -EINVAL; |
| 566 | } |
| 567 | |
| 568 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 569 | LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | /* Section 7.3 */ |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 575 | static int gprs_ns_rx_reset(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 576 | { |
| 577 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 578 | struct tlv_parsed tp; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 579 | uint8_t *cause; |
| 580 | uint16_t *nsvci, *nsei; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 581 | int rc; |
| 582 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 583 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 584 | |
| 585 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 586 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 587 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 588 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 589 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 590 | return -EINVAL; |
| 591 | } |
| 592 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 593 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 594 | nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 595 | nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 596 | |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 597 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET (NSVCI=%u, cause=%s)\n", |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 598 | nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause)); |
| 599 | |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 600 | /* Mark NS-VC as blocked and alive */ |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 601 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | d7c02ad | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 602 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 603 | nsvc->nsei = ntohs(*nsei); |
| 604 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 605 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 606 | /* start the test procedure */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 607 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 608 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 609 | /* inform interested parties about the fact that this NSVC |
| 610 | * has received RESET */ |
Harald Welte | 1389ac7 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 611 | ns_dispatch_signal(nsvc, S_NS_RESET, *cause); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 612 | |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 613 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 614 | } |
| 615 | |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 616 | static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 617 | { |
| 618 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 619 | struct tlv_parsed tp; |
| 620 | uint8_t *cause; |
| 621 | int rc; |
| 622 | |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 623 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 624 | |
| 625 | nsvc->state |= NSE_S_BLOCKED; |
| 626 | |
| 627 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 628 | |
| 629 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 630 | !TLVP_PRESENT(&tp, NS_IE_VCI)) { |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 631 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 632 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 633 | return -EINVAL; |
| 634 | } |
| 635 | |
| 636 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 637 | //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 638 | |
Harald Welte | 1389ac7 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 639 | ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 640 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 641 | |
| 642 | return gprs_ns_tx_simple(nsvc, NS_PDUT_BLOCK_ACK); |
| 643 | } |
| 644 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 645 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 646 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 647 | struct sockaddr_in *saddr) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 648 | { |
| 649 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 650 | struct gprs_nsvc *nsvc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 651 | int rc = 0; |
| 652 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 653 | /* look up the NSVC based on source address */ |
| 654 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 655 | if (!nsvc) { |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 656 | struct tlv_parsed tp; |
| 657 | uint16_t nsei; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 658 | /* Only the RESET procedure creates a new NSVC */ |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 659 | if (nsh->pdu_type != NS_PDUT_RESET) { |
Harald Welte | 9aa97fc | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 660 | /* Since we have no NSVC, we have to use a fake */ |
| 661 | nsvc = nsi->unknown_nsvc; |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 662 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | 9aa97fc | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 663 | LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type 0x%0x " |
Harald Welte | ef1226e | 2010-05-11 18:38:36 +0200 | [diff] [blame] | 664 | "from %s:%u for non-existing NS-VC\n", |
| 665 | nsh->pdu_type, inet_ntoa(saddr->sin_addr), |
| 666 | ntohs(saddr->sin_port)); |
Harald Welte | 9aa97fc | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 667 | nsvc->nsvci = nsvc->nsei = 0xfffe; |
| 668 | nsvc->ip.bts_addr = *saddr; |
| 669 | nsvc->state = NSE_S_ALIVE; |
| 670 | return gprs_ns_tx_status(nsvc, |
Harald Welte | 8c2440e | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 671 | NS_CAUSE_PDU_INCOMP_PSTATE, 0, |
| 672 | msg); |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 673 | } |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 674 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 675 | msgb_l2len(msg), 0, 0); |
| 676 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 677 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 678 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 679 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 680 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
| 681 | msg); |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 682 | return -EINVAL; |
| 683 | } |
| 684 | nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI)); |
| 685 | /* Check if we already know this NSEI, the remote end might |
| 686 | * simply have changed addresses, or it is a SGSN */ |
| 687 | nsvc = nsvc_by_nsei(nsi, nsei); |
| 688 | if (!nsvc) { |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 689 | nsvc = nsvc_create(nsi, 0xffff); |
| 690 | log_set_context(BSC_CTX_NSVC, nsvc); |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 691 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n", |
| 692 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
Harald Welte | e5117da | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 693 | } |
Harald Welte | 7c209eb | 2010-05-11 18:40:45 +0200 | [diff] [blame] | 694 | /* Update the remote peer IP address/port */ |
| 695 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 696 | } else |
| 697 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 698 | |
Harald Welte | 8be8c8f | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 699 | log_set_context(BSC_CTX_NSVC, nsvc); |
| 700 | |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 701 | /* Increment number of Incoming bytes */ |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 702 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]); |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 703 | rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg)); |
Harald Welte | f2b4cd7 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 704 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 705 | switch (nsh->pdu_type) { |
| 706 | case NS_PDUT_ALIVE: |
Harald Welte | 1194b58 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 707 | /* If we're dead and blocked and suddenly receive a |
| 708 | * NS-ALIVE out of the blue, we might have been re-started |
| 709 | * and should send a NS-RESET to make sure everything recovers |
| 710 | * fine. */ |
| 711 | if (nsvc->state == NSE_S_BLOCKED) |
| 712 | rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE); |
| 713 | else |
| 714 | rc = gprs_ns_tx_alive_ack(nsvc); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 715 | break; |
| 716 | case NS_PDUT_ALIVE_ACK: |
Harald Welte | ff56d61 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 717 | /* stop Tns-alive and start Tns-test */ |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 718 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 24b3131 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 719 | if (nsvc->remote_end_is_sgsn) { |
| 720 | /* FIXME: this should be one level higher */ |
| 721 | if (nsvc->state & NSE_S_BLOCKED) |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 722 | rc = gprs_ns_tx_unblock(nsvc); |
Harald Welte | 24b3131 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 723 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 724 | break; |
| 725 | case NS_PDUT_UNITDATA: |
| 726 | /* actual user data */ |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 727 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 728 | break; |
| 729 | case NS_PDUT_STATUS: |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 730 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 731 | break; |
| 732 | case NS_PDUT_RESET: |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 733 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 734 | break; |
| 735 | case NS_PDUT_RESET_ACK: |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 736 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 737 | /* mark NS-VC as blocked + active */ |
| 738 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 739 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | c191986 | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 740 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 741 | if (nsvc->persistent || nsvc->remote_end_is_sgsn) { |
Harald Welte | 199d9df | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 742 | /* stop RESET timer */ |
| 743 | bsc_del_timer(&nsvc->timer); |
| 744 | } |
Harald Welte | ff56d61 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 745 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
| 746 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 747 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 748 | break; |
| 749 | case NS_PDUT_UNBLOCK: |
| 750 | /* Section 7.2: unblocking procedure */ |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 751 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 752 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 753 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 5434d7e | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 754 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 755 | break; |
| 756 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 757 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei); |
Harald Welte | 9bdb3ba | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 758 | /* mark NS-VC as unblocked + active */ |
| 759 | nsvc->state = NSE_S_ALIVE; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 760 | nsvc->remote_state = NSE_S_ALIVE; |
Harald Welte | f6d67c0 | 2010-05-12 14:18:46 +0000 | [diff] [blame] | 761 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 762 | break; |
| 763 | case NS_PDUT_BLOCK: |
Harald Welte | 99e3248 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 764 | rc = gprs_ns_rx_block(nsvc, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 765 | break; |
| 766 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 767 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 768 | /* mark remote NS-VC as blocked + active */ |
| 769 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 770 | break; |
| 771 | default: |
Harald Welte | fcc4cc9 | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 772 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n", |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 773 | nsvc->nsei, nsh->pdu_type); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 774 | rc = -EINVAL; |
| 775 | break; |
| 776 | } |
| 777 | return rc; |
| 778 | } |
| 779 | |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 780 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb) |
| 781 | { |
| 782 | struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst); |
| 783 | |
| 784 | nsi->cb = cb; |
| 785 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
Harald Welte | ea4647d | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 786 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 787 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 788 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 789 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 790 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 791 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 792 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 793 | |
Harald Welte | 3863e04 | 2010-05-13 14:20:56 +0200 | [diff] [blame] | 794 | /* Create the dummy NSVC that we use for sending |
| 795 | * messages to non-existant/unknown NS-VC's */ |
Harald Welte | 9aa97fc | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 796 | nsi->unknown_nsvc = nsvc_create(nsi, 0xfffe); |
Harald Welte | 3863e04 | 2010-05-13 14:20:56 +0200 | [diff] [blame] | 797 | llist_del(&nsi->unknown_nsvc->list); |
Harald Welte | 9aa97fc | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 798 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 799 | return nsi; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 803 | { |
| 804 | /* FIXME: clear all timers */ |
| 805 | |
| 806 | /* recursively free the NSI and all its NSVCs */ |
| 807 | talloc_free(nsi); |
| 808 | } |
| 809 | |
| 810 | |
| 811 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 812 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 813 | |
| 814 | /* Read a single NS-over-IP message */ |
| 815 | static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error, |
| 816 | struct sockaddr_in *saddr) |
| 817 | { |
| 818 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS"); |
| 819 | int ret = 0; |
| 820 | socklen_t saddr_len = sizeof(*saddr); |
| 821 | |
| 822 | if (!msg) { |
| 823 | *error = -ENOMEM; |
| 824 | return NULL; |
| 825 | } |
| 826 | |
| 827 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 828 | (struct sockaddr *)saddr, &saddr_len); |
| 829 | if (ret < 0) { |
Harald Welte | 6b72cdf | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 830 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
Harald Welte | 239cf05 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 831 | strerror(errno)); |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 832 | msgb_free(msg); |
| 833 | *error = ret; |
| 834 | return NULL; |
| 835 | } else if (ret == 0) { |
| 836 | msgb_free(msg); |
| 837 | *error = ret; |
| 838 | return NULL; |
| 839 | } |
| 840 | |
| 841 | msg->l2h = msg->data; |
| 842 | msgb_put(msg, ret); |
| 843 | |
| 844 | return msg; |
| 845 | } |
| 846 | |
| 847 | static int handle_nsip_read(struct bsc_fd *bfd) |
| 848 | { |
| 849 | int error; |
| 850 | struct sockaddr_in saddr; |
| 851 | struct gprs_ns_inst *nsi = bfd->data; |
| 852 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 853 | |
| 854 | if (!msg) |
| 855 | return error; |
| 856 | |
Harald Welte | 8272c77 | 2010-05-12 18:38:45 +0000 | [diff] [blame] | 857 | error = gprs_ns_rcvmsg(nsi, msg, &saddr); |
| 858 | |
| 859 | msgb_free(msg); |
| 860 | |
| 861 | return error; |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | static int handle_nsip_write(struct bsc_fd *bfd) |
| 865 | { |
| 866 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 867 | return -EIO; |
| 868 | } |
| 869 | |
Harald Welte | 44f1c27 | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 870 | int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 871 | { |
| 872 | int rc; |
| 873 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 874 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 875 | |
| 876 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 877 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 878 | |
| 879 | talloc_free(msg); |
| 880 | |
| 881 | return rc; |
| 882 | } |
| 883 | |
| 884 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
| 885 | static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 886 | { |
| 887 | int rc = 0; |
| 888 | |
| 889 | if (what & BSC_FD_READ) |
| 890 | rc = handle_nsip_read(bfd); |
| 891 | if (what & BSC_FD_WRITE) |
| 892 | rc = handle_nsip_write(bfd); |
| 893 | |
| 894 | return rc; |
| 895 | } |
| 896 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 897 | extern int make_sock(struct bsc_fd *bfd, int proto, uint16_t port, |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 898 | int (*cb)(struct bsc_fd *fd, unsigned int what)); |
| 899 | |
| 900 | /* Listen for incoming GPRS packets */ |
| 901 | int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port) |
| 902 | { |
| 903 | int ret; |
| 904 | |
| 905 | ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb); |
| 906 | if (ret < 0) |
| 907 | return ret; |
| 908 | |
| 909 | nsi->ll = GPRS_NS_LL_UDP; |
| 910 | nsi->nsip.fd.data = nsi; |
| 911 | |
| 912 | return ret; |
| 913 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 914 | |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 915 | /* Initiate a RESET procedure */ |
| 916 | int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
| 917 | { |
| 918 | /* Mark NS-VC locally as blocked and dead */ |
| 919 | nsvc->state = NSE_S_BLOCKED; |
| 920 | /* Send NS-RESET PDU */ |
| 921 | if (gprs_ns_tx_reset(nsvc, cause) < 0) { |
| 922 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
| 923 | nsvc->nsei); |
| 924 | } |
| 925 | /* Start Tns-reset */ |
| 926 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
| 927 | |
| 928 | return nsvc; |
| 929 | } |
| 930 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 931 | /* Establish a connection (from the BSS) to the SGSN */ |
| 932 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 933 | struct sockaddr_in *dest, uint16_t nsei, |
| 934 | uint16_t nsvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 935 | { |
| 936 | struct gprs_nsvc *nsvc; |
| 937 | |
| 938 | nsvc = nsvc_by_rem_addr(nsi, dest); |
Harald Welte | cad8301 | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 939 | if (!nsvc) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 940 | nsvc = nsvc_create(nsi, nsvci); |
Harald Welte | cad8301 | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 941 | nsvc->ip.bts_addr = *dest; |
Harald Welte | b77c697 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 942 | nsvc->nsei = nsei; |
| 943 | nsvc->nsvci = nsvci; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 944 | nsvc->remote_end_is_sgsn = 1; |
| 945 | |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 946 | return gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 947 | } |