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