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