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