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 | c80bb28 | 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 | f67a5f9 | 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 93 | uint16_t nsvci) |
Harald Welte | f67a5f9 | 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 | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 103 | /* Lookup struct gprs_nsvc based on NSVCI */ |
Harald Welte | c80bb28 | 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 | cd788ba | 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 | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 114 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 115 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 116 | struct sockaddr_in *sin) |
| 117 | { |
| 118 | struct gprs_nsvc *nsvc; |
| 119 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
Harald Welte | 517b77f | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 120 | if (nsvc->ip.bts_addr.sin_addr.s_addr == |
| 121 | sin->sin_addr.s_addr && |
| 122 | nsvc->ip.bts_addr.sin_port == sin->sin_port) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 123 | return nsvc; |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 128 | static void gprs_ns_timer_cb(void *data); |
| 129 | |
Harald Welte | c80bb28 | 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 | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 131 | { |
| 132 | struct gprs_nsvc *nsvc; |
| 133 | |
| 134 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 135 | nsvc->nsvci = nsvci; |
| 136 | /* before RESET procedure: BLOCKED and DEAD */ |
| 137 | nsvc->state = NSE_S_BLOCKED; |
| 138 | nsvc->nsi = nsi; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 139 | nsvc->timer.cb = gprs_ns_timer_cb; |
| 140 | nsvc->timer.data = nsvc; |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 141 | nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 142 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 143 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 144 | |
| 145 | return nsvc; |
| 146 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 147 | |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 148 | void nsvc_delete(struct gprs_nsvc *nsvc) |
Harald Welte | 51cce2a | 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 | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 156 | static void ns_dispatch_signal(struct gprs_nsvc *nsvc, unsigned int signal, |
Harald Welte | 7b73377 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 167 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 5fa9214 | 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 | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 177 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 5fa9214 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 181 | }; |
| 182 | |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 183 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 184 | { |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 185 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 186 | } |
| 187 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 188 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 189 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 190 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 191 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 192 | int ret; |
| 193 | |
Harald Welte | c80bb28 | 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 | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 198 | switch (nsvc->nsi->ll) { |
| 199 | case GPRS_NS_LL_UDP: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 200 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 201 | break; |
| 202 | default: |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 203 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 204 | msgb_free(msg); |
| 205 | ret = -EIO; |
| 206 | break; |
| 207 | } |
| 208 | return ret; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 209 | } |
| 210 | |
Harald Welte | d85d9a9 | 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 | 75bb820 | 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 | c80bb28 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 221 | |
| 222 | nsh->pdu_type = pdu_type; |
| 223 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 224 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 225 | } |
| 226 | |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 227 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | ec4c159 | 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 | ec4c159 | 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 | e21988c | 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 | c80bb28 | 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 | ec4c159 | 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 | dae2ab3 | 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 | e21988c | 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 | c80bb28 | 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 | dae2ab3 | 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 | 7b73377 | 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 | e21988c | 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 | 7b73377 | 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 | c80bb28 | 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 | 7b73377 | 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 | e21988c | 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 | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 329 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 330 | } |
| 331 | |
Harald Welte | 9a4847f | 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 | d6b62e3 | 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 | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 352 | }; |
| 353 | |
Harald Welte | 6ec997f | 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 | ec4c159 | 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 | d6b62e3 | 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 | 4574420 | 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 | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 368 | seconds); |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 369 | |
Harald Welte | ec4c159 | 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 | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 374 | bsc_schedule_timer(&nsvc->timer, seconds, 0); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 377 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 378 | { |
| 379 | struct gprs_nsvc *nsvc = data; |
Harald Welte | d6b62e3 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 382 | |
Harald Welte | 4574420 | 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 | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 385 | seconds); |
Harald Welte | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 386 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 387 | switch (nsvc->timer_mode) { |
| 388 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 389 | /* Tns-alive case: we expired without response ! */ |
| 390 | nsvc->alive_retries++; |
Harald Welte | d6b62e3 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 393 | /* mark as dead and blocked */ |
| 394 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 395 | LOGP(DNS, LOGL_NOTICE, |
| 396 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 397 | "%u times, blocking NS-VC\n", nsvc->nsei, |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 398 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]); |
Harald Welte | fa41cb7 | 2010-05-12 13:28:25 +0000 | [diff] [blame] | 399 | ns_dispatch_signal(nsvc, S_NS_ALIVE_EXP, 0); |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 400 | ns_dispatch_signal(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 401 | return; |
| 402 | } |
Harald Welte | 9a4847f | 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 | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 406 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 407 | break; |
| 408 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 409 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | 9a4847f | 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 | 6ec997f | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 413 | nsvc->alive_retries = 0; |
Harald Welte | ec4c159 | 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 | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 418 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 419 | /* Re-start Tns-reset timer */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 420 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 421 | break; |
Harald Welte | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 422 | case _NSVC_TIMER_NR: |
| 423 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 424 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* Section 9.2.6 */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 428 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 75bb820 | 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 432 | uint16_t nsvci, nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 433 | |
| 434 | if (!msg) |
| 435 | return -ENOMEM; |
| 436 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 437 | nsvci = htons(nsvc->nsvci); |
| 438 | nsei = htons(nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 439 | |
Harald Welte | c80bb28 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 442 | |
| 443 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 444 | |
Harald Welte | c729e2b | 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 | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 446 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 447 | |
Harald Welte | d85d9a9 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 450 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 451 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 452 | } |
| 453 | |
Harald Welte | cd788ba | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 456 | { |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 457 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 458 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 459 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 460 | |
| 461 | nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg)); |
| 462 | if (!nsvc) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 463 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 464 | "to NS-VC!\n", msgb_nsei(msg)); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 465 | return -EINVAL; |
| 466 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 467 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 468 | if (!(nsvc->state & NSE_S_ALIVE)) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 469 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n", |
Harald Welte | ec4c159 | 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 | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 474 | LOGP(DNS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 475 | nsvc->nsei); |
| 476 | return -EBUSY; |
| 477 | } |
| 478 | |
Harald Welte | 75bb820 | 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 | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 481 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | 75bb820 | 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 | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 490 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /* Section 9.2.10: receive side */ |
Harald Welte | cd788ba | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 495 | { |
| 496 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 497 | uint16_t bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 498 | |
Harald Welte | dae2ab3 | 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 | 75bb820 | 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 | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 505 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 506 | msgb_bvci(msg) = bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 507 | |
| 508 | /* call upper layer (BSSGP) */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 509 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* Section 9.2.7 */ |
Harald Welte | cd788ba | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 514 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 515 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 516 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 517 | uint8_t cause; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 518 | int rc; |
| 519 | |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 520 | LOGP(DNS, LOGL_INFO, "NSEI=%u NS STATUS ", nsvc->nsei); |
Harald Welte | 75bb820 | 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 | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 525 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 530 | LOGPC(DNS, LOGL_INFO, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* Section 7.3 */ |
Harald Welte | cd788ba | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 537 | { |
| 538 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 539 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 540 | uint8_t *cause; |
| 541 | uint16_t *nsvci, *nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 542 | int rc; |
| 543 | |
Harald Welte | 75bb820 | 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 | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 549 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 550 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 551 | return -EINVAL; |
| 552 | } |
| 553 | |
Harald Welte | d85d9a9 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 557 | |
Harald Welte | c729e2b | 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 | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 559 | nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause)); |
| 560 | |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 561 | /* Mark NS-VC as blocked and alive */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 562 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f44448d | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 563 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 564 | nsvc->nsei = ntohs(*nsei); |
| 565 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 566 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 567 | /* start the test procedure */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 568 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 569 | |
Harald Welte | 7b73377 | 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 | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 572 | ns_dispatch_signal(nsvc, S_NS_RESET, *cause); |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 573 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 574 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 575 | } |
| 576 | |
Harald Welte | 7b73377 | 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 | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 584 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 7b73377 | 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 | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 592 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 593 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 7b73377 | 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 | 73c7b17 | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 600 | ns_dispatch_signal(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | 7b73377 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 605 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | f67a5f9 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 608 | { |
| 609 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 610 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 611 | int rc = 0; |
| 612 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 613 | /* look up the NSVC based on source address */ |
| 614 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 615 | if (!nsvc) { |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 616 | struct tlv_parsed tp; |
| 617 | uint16_t nsei; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 618 | /* Only the RESET procedure creates a new NSVC */ |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 619 | if (nsh->pdu_type != NS_PDUT_RESET) { |
Harald Welte | 78681af | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 620 | struct gprs_nsvc fake_nsvc; |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 621 | LOGP(DNS, LOGL_INFO, "Ignoring NS PDU type 0x%0x " |
Harald Welte | c8c3de3 | 2010-05-11 18:38:36 +0200 | [diff] [blame] | 622 | "from %s:%u for non-existing NS-VC\n", |
| 623 | nsh->pdu_type, inet_ntoa(saddr->sin_addr), |
| 624 | ntohs(saddr->sin_port)); |
Harald Welte | 78681af | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 625 | /* Since we have no NSVC, we have to create a fake */ |
| 626 | fake_nsvc.nsvci = fake_nsvc.nsei = 0; |
| 627 | fake_nsvc.nsi = nsi; |
| 628 | fake_nsvc.ip.bts_addr = *saddr; |
| 629 | fake_nsvc.state = NSE_S_ALIVE; |
Harald Welte | 78681af | 2010-05-12 13:51:08 +0000 | [diff] [blame] | 630 | return gprs_ns_tx_status(&fake_nsvc, |
| 631 | NS_CAUSE_PDU_INCOMP_PSTATE, 0, |
| 632 | msg); |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 633 | } |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 634 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 635 | msgb_l2len(msg), 0, 0); |
| 636 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 637 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 638 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 639 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 640 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
| 641 | msg); |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 642 | return -EINVAL; |
| 643 | } |
| 644 | nsei = ntohs(*(uint16_t *)TLVP_VAL(&tp, NS_IE_NSEI)); |
| 645 | /* Check if we already know this NSEI, the remote end might |
| 646 | * simply have changed addresses, or it is a SGSN */ |
| 647 | nsvc = nsvc_by_nsei(nsi, nsei); |
| 648 | if (!nsvc) { |
| 649 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n", |
| 650 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
| 651 | nsvc = nsvc_create(nsi, 0xffff); |
Harald Welte | d2e5ed0 | 2010-05-11 18:30:37 +0200 | [diff] [blame] | 652 | } |
Harald Welte | 19b96b9 | 2010-05-11 18:40:45 +0200 | [diff] [blame] | 653 | /* Update the remote peer IP address/port */ |
| 654 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 655 | } else |
| 656 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 657 | |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 658 | /* Increment number of Incoming bytes */ |
Harald Welte | 73caf1f | 2010-05-13 11:57:44 +0200 | [diff] [blame^] | 659 | rate_ctr_inc(&nsvc->ctrg->ctr[0]); |
| 660 | rate_ctr_add(&nsvc->ctrg->ctr[2], msgb_l2len(msg)); |
Harald Welte | c80bb28 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 661 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 662 | switch (nsh->pdu_type) { |
| 663 | case NS_PDUT_ALIVE: |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 664 | /* If we're dead and blocked and suddenly receive a |
| 665 | * NS-ALIVE out of the blue, we might have been re-started |
| 666 | * and should send a NS-RESET to make sure everything recovers |
| 667 | * fine. */ |
| 668 | if (nsvc->state == NSE_S_BLOCKED) |
| 669 | rc = gprs_ns_tx_reset(nsvc, NS_CAUSE_PDU_INCOMP_PSTATE); |
| 670 | else |
| 671 | rc = gprs_ns_tx_alive_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 672 | break; |
| 673 | case NS_PDUT_ALIVE_ACK: |
| 674 | /* stop Tns-alive */ |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 675 | bsc_del_timer(&nsvc->timer); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 676 | /* start Tns-test */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 677 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 1bb9e79 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 678 | if (nsvc->remote_end_is_sgsn) { |
| 679 | /* FIXME: this should be one level higher */ |
| 680 | if (nsvc->state & NSE_S_BLOCKED) |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 681 | rc = gprs_ns_tx_unblock(nsvc); |
Harald Welte | 1bb9e79 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 682 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 683 | break; |
| 684 | case NS_PDUT_UNITDATA: |
| 685 | /* actual user data */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 686 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 687 | break; |
| 688 | case NS_PDUT_STATUS: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 689 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 690 | break; |
| 691 | case NS_PDUT_RESET: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 692 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 693 | break; |
| 694 | case NS_PDUT_RESET_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 695 | 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] | 696 | /* mark NS-VC as blocked + active */ |
| 697 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 698 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 699 | if (nsvc->remote_end_is_sgsn) { |
| 700 | /* stop RESET timer */ |
| 701 | bsc_del_timer(&nsvc->timer); |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 702 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 703 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 704 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 705 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 706 | break; |
| 707 | case NS_PDUT_UNBLOCK: |
| 708 | /* Section 7.2: unblocking procedure */ |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 709 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 710 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 711 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 712 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 713 | break; |
| 714 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 715 | 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] | 716 | /* mark NS-VC as unblocked + active */ |
| 717 | nsvc->state = NSE_S_ALIVE; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 718 | nsvc->remote_state = NSE_S_ALIVE; |
Harald Welte | 4108b38 | 2010-05-12 14:18:46 +0000 | [diff] [blame] | 719 | ns_dispatch_signal(nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 720 | break; |
| 721 | case NS_PDUT_BLOCK: |
Harald Welte | 7b73377 | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 722 | rc = gprs_ns_rx_block(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 723 | break; |
| 724 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 725 | 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] | 726 | /* mark remote NS-VC as blocked + active */ |
| 727 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 728 | break; |
| 729 | default: |
Harald Welte | c729e2b | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 730 | 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] | 731 | nsvc->nsei, nsh->pdu_type); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 732 | rc = -EINVAL; |
| 733 | break; |
| 734 | } |
| 735 | return rc; |
| 736 | } |
| 737 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 738 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb) |
| 739 | { |
| 740 | struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst); |
| 741 | |
| 742 | nsi->cb = cb; |
| 743 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
Harald Welte | d6b62e3 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 744 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 745 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 746 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 747 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 748 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 749 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 750 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 751 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 752 | return nsi; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 756 | { |
| 757 | /* FIXME: clear all timers */ |
| 758 | |
| 759 | /* recursively free the NSI and all its NSVCs */ |
| 760 | talloc_free(nsi); |
| 761 | } |
| 762 | |
| 763 | |
| 764 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 765 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 766 | |
| 767 | /* Read a single NS-over-IP message */ |
| 768 | static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error, |
| 769 | struct sockaddr_in *saddr) |
| 770 | { |
| 771 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS"); |
| 772 | int ret = 0; |
| 773 | socklen_t saddr_len = sizeof(*saddr); |
| 774 | |
| 775 | if (!msg) { |
| 776 | *error = -ENOMEM; |
| 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 781 | (struct sockaddr *)saddr, &saddr_len); |
| 782 | if (ret < 0) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 783 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 784 | strerror(errno)); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 785 | msgb_free(msg); |
| 786 | *error = ret; |
| 787 | return NULL; |
| 788 | } else if (ret == 0) { |
| 789 | msgb_free(msg); |
| 790 | *error = ret; |
| 791 | return NULL; |
| 792 | } |
| 793 | |
| 794 | msg->l2h = msg->data; |
| 795 | msgb_put(msg, ret); |
| 796 | |
| 797 | return msg; |
| 798 | } |
| 799 | |
| 800 | static int handle_nsip_read(struct bsc_fd *bfd) |
| 801 | { |
| 802 | int error; |
| 803 | struct sockaddr_in saddr; |
| 804 | struct gprs_ns_inst *nsi = bfd->data; |
| 805 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 806 | |
| 807 | if (!msg) |
| 808 | return error; |
| 809 | |
Harald Welte | d8f2271 | 2010-05-12 18:38:45 +0000 | [diff] [blame] | 810 | error = gprs_ns_rcvmsg(nsi, msg, &saddr); |
| 811 | |
| 812 | msgb_free(msg); |
| 813 | |
| 814 | return error; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | static int handle_nsip_write(struct bsc_fd *bfd) |
| 818 | { |
| 819 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 820 | return -EIO; |
| 821 | } |
| 822 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 823 | int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 824 | { |
| 825 | int rc; |
| 826 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 827 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 828 | |
| 829 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 830 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 831 | |
| 832 | talloc_free(msg); |
| 833 | |
| 834 | return rc; |
| 835 | } |
| 836 | |
| 837 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
| 838 | static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 839 | { |
| 840 | int rc = 0; |
| 841 | |
| 842 | if (what & BSC_FD_READ) |
| 843 | rc = handle_nsip_read(bfd); |
| 844 | if (what & BSC_FD_WRITE) |
| 845 | rc = handle_nsip_write(bfd); |
| 846 | |
| 847 | return rc; |
| 848 | } |
| 849 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 850 | 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] | 851 | int (*cb)(struct bsc_fd *fd, unsigned int what)); |
| 852 | |
| 853 | /* Listen for incoming GPRS packets */ |
| 854 | int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port) |
| 855 | { |
| 856 | int ret; |
| 857 | |
| 858 | ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb); |
| 859 | if (ret < 0) |
| 860 | return ret; |
| 861 | |
| 862 | nsi->ll = GPRS_NS_LL_UDP; |
| 863 | nsi->nsip.fd.data = nsi; |
| 864 | |
| 865 | return ret; |
| 866 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 867 | |
| 868 | /* Establish a connection (from the BSS) to the SGSN */ |
| 869 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 870 | struct sockaddr_in *dest, uint16_t nsei, |
| 871 | uint16_t nsvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 872 | { |
| 873 | struct gprs_nsvc *nsvc; |
| 874 | |
| 875 | nsvc = nsvc_by_rem_addr(nsi, dest); |
Harald Welte | 517b77f | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 876 | if (!nsvc) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 877 | nsvc = nsvc_create(nsi, nsvci); |
Harald Welte | 517b77f | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 878 | nsvc->ip.bts_addr = *dest; |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 879 | nsvc->nsei = nsei; |
| 880 | nsvc->nsvci = nsvci; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 881 | nsvc->remote_end_is_sgsn = 1; |
| 882 | |
| 883 | /* Initiate a RESET procedure */ |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 884 | /* Mark NS-VC locally as blocked and dead */ |
| 885 | nsvc->state = NSE_S_BLOCKED; |
| 886 | /* Send NS-RESET PDU */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 887 | if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) { |
Harald Welte | ae14132 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 888 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 889 | nsei); |
| 890 | } |
Harald Welte | dae2ab3 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 891 | /* Start Tns-reset */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 892 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 893 | |
| 894 | return nsvc; |
| 895 | } |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 896 | |
Harald Welte | 51cce2a | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 897 | |