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