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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 59 | #include <openbsc/debug.h> |
| 60 | #include <openbsc/gprs_ns.h> |
| 61 | #include <openbsc/gprs_bssgp.h> |
| 62 | |
| 63 | #define NS_ALLOC_SIZE 1024 |
| 64 | |
| 65 | static const struct tlv_definition ns_att_tlvdef = { |
| 66 | .def = { |
| 67 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 68 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 69 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 70 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 71 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 72 | }, |
| 73 | }; |
| 74 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 75 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 76 | static struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 77 | uint16_t nsvci) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 78 | { |
| 79 | struct gprs_nsvc *nsvc; |
| 80 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 81 | if (nsvc->nsvci == nsvci) |
| 82 | return nsvc; |
| 83 | } |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 87 | /* Lookup struct gprs_nsvc based on NSVCI */ |
| 88 | static struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 89 | uint16_t nsei) |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 90 | { |
| 91 | struct gprs_nsvc *nsvc; |
| 92 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 93 | if (nsvc->nsei == nsei) |
| 94 | return nsvc; |
| 95 | } |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 100 | /* Lookup struct gprs_nsvc based on remote peer socket addr */ |
| 101 | static struct gprs_nsvc *nsvc_by_rem_addr(struct gprs_ns_inst *nsi, |
| 102 | struct sockaddr_in *sin) |
| 103 | { |
| 104 | struct gprs_nsvc *nsvc; |
| 105 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 106 | if (!memcmp(&nsvc->ip.bts_addr, sin, sizeof(*sin))) |
| 107 | return nsvc; |
| 108 | } |
| 109 | return NULL; |
| 110 | } |
| 111 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 112 | static void gprs_ns_timer_cb(void *data); |
| 113 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 114 | static 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] | 115 | { |
| 116 | struct gprs_nsvc *nsvc; |
| 117 | |
| 118 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
| 119 | nsvc->nsvci = nsvci; |
| 120 | /* before RESET procedure: BLOCKED and DEAD */ |
| 121 | nsvc->state = NSE_S_BLOCKED; |
| 122 | nsvc->nsi = nsi; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 123 | nsvc->timer.cb = gprs_ns_timer_cb; |
| 124 | nsvc->timer.data = nsvc; |
| 125 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 126 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 127 | |
| 128 | return nsvc; |
| 129 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 130 | |
| 131 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 132 | static const struct value_string ns_cause_str[] = { |
| 133 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 134 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 135 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 136 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 137 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 138 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 139 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 140 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 141 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 142 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 143 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 144 | { 0, NULL } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 145 | }; |
| 146 | |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 147 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 148 | { |
Harald Welte | 5fa9214 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 149 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 150 | } |
| 151 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 152 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 153 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 154 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 155 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 156 | int ret; |
| 157 | |
| 158 | switch (nsvc->nsi->ll) { |
| 159 | case GPRS_NS_LL_UDP: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 160 | ret = nsip_sendmsg(nsvc, msg); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 161 | break; |
| 162 | default: |
| 163 | LOGP(DGPRS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->nsi->ll); |
| 164 | msgb_free(msg); |
| 165 | ret = -EIO; |
| 166 | break; |
| 167 | } |
| 168 | return ret; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 171 | 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] | 172 | { |
| 173 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 174 | struct gprs_ns_hdr *nsh; |
| 175 | |
| 176 | if (!msg) |
| 177 | return -ENOMEM; |
| 178 | |
| 179 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 180 | |
| 181 | nsh->pdu_type = pdu_type; |
| 182 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 183 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame^] | 186 | static int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 187 | { |
| 188 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 189 | struct gprs_ns_hdr *nsh; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 190 | uint16_t nsvci = htons(nsvc->nsvci); |
| 191 | uint16_t nsei = htons(nsvc->nsei); |
| 192 | |
| 193 | if (!msg) |
| 194 | return -ENOMEM; |
| 195 | |
| 196 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 197 | nsh->pdu_type = NS_PDUT_RESET; |
| 198 | |
| 199 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 200 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 201 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 202 | |
| 203 | return gprs_ns_tx(nsvc, msg); |
| 204 | |
| 205 | } |
| 206 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 207 | #define NS_ALIVE_RETRIES 10 /* after 3 failed retransmit we declare BTS as dead */ |
| 208 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 209 | static const uint8_t timer_mode_tout[_NSVC_TIMER_NR] = { |
| 210 | [NSVC_TIMER_TNS_RESET] = 60, |
| 211 | [NSVC_TIMER_TNS_ALIVE] = 3, |
| 212 | [NSVC_TIMER_TNS_TEST] = 30, |
| 213 | }; |
| 214 | |
| 215 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 216 | { |
| 217 | nsvc->alive_retries = 0; |
| 218 | |
| 219 | if (bsc_timer_pending(&nsvc->timer)) |
| 220 | bsc_del_timer(&nsvc->timer); |
| 221 | |
| 222 | nsvc->timer_mode = mode; |
| 223 | bsc_schedule_timer(&nsvc->timer, timer_mode_tout[mode], 0); |
| 224 | } |
| 225 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 226 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 227 | { |
| 228 | struct gprs_nsvc *nsvc = data; |
| 229 | |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 230 | switch (nsvc->timer_mode) { |
| 231 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 232 | /* Tns-alive case: we expired without response ! */ |
| 233 | nsvc->alive_retries++; |
| 234 | if (nsvc->alive_retries > NS_ALIVE_RETRIES) { |
| 235 | /* mark as dead and blocked */ |
| 236 | nsvc->state = NSE_S_BLOCKED; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 237 | DEBUGP(DGPRS, "NSEI=%u Tns-alive expired more then " |
| 238 | "%u times, blocking NS-VC\n", nsvc->nsei, |
| 239 | NS_ALIVE_RETRIES); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 240 | /* FIXME: inform higher layers */ |
| 241 | return; |
| 242 | } |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 243 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 244 | break; |
| 245 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 246 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 247 | gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 248 | /* start Tns-alive timer */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 249 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 250 | break; |
| 251 | case NSVC_TIMER_TNS_RESET: |
| 252 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame^] | 253 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 254 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 255 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 256 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* Section 9.2.6 */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 260 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 261 | { |
| 262 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "GPRS/NS"); |
| 263 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 264 | uint16_t nsvci, nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 265 | |
| 266 | if (!msg) |
| 267 | return -ENOMEM; |
| 268 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 269 | nsvci = htons(nsvc->nsvci); |
| 270 | nsei = htons(nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 271 | |
| 272 | nsh = (struct gprs_ns_hdr *) msgb_put(msg, sizeof(*nsh)); |
| 273 | |
| 274 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 275 | |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 276 | DEBUGP(DGPRS, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n", |
| 277 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 278 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 279 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 280 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 281 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 282 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 283 | } |
| 284 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 285 | /* Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive */ |
| 286 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 287 | { |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 288 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 289 | struct gprs_ns_hdr *nsh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 290 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 291 | |
| 292 | nsvc = nsvc_by_nsei(nsi, msgb_nsei(msg)); |
| 293 | if (!nsvc) { |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 294 | LOGP(DGPRS, LOGL_ERROR, "Unable to resolve NSEI %u " |
| 295 | "to NS-VC!\n", msgb_nsei(msg)); |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 296 | return -EINVAL; |
| 297 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 298 | |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 299 | if (!(nsvc->state & NSE_S_ALIVE)) { |
| 300 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u is not alive, cannot send\n", |
| 301 | nsvc->nsei); |
| 302 | return -EBUSY; |
| 303 | } |
| 304 | if (nsvc->state & NSE_S_BLOCKED) { |
| 305 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u is blocked, cannot send\n", |
| 306 | nsvc->nsei); |
| 307 | return -EBUSY; |
| 308 | } |
| 309 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 310 | nsh = (struct gprs_ns_hdr *) msgb_push(msg, sizeof(*nsh) + 3); |
| 311 | if (!nsh) { |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 312 | LOGP(DGPRS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 313 | return -EIO; |
| 314 | } |
| 315 | |
| 316 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 317 | /* spare octet in data[0] */ |
| 318 | nsh->data[1] = bvci >> 8; |
| 319 | nsh->data[2] = bvci & 0xff; |
| 320 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 321 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* Section 9.2.10: receive side */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 325 | 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] | 326 | { |
| 327 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 328 | uint16_t bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 329 | |
| 330 | /* spare octet in data[0] */ |
| 331 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | 7316440 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 332 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 333 | msgb_bvci(msg) = bvci; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 334 | |
| 335 | /* call upper layer (BSSGP) */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 336 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Section 9.2.7 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 340 | 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] | 341 | { |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 342 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 343 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 344 | uint8_t cause; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 345 | int rc; |
| 346 | |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 347 | DEBUGP(DGPRS, "NSEI=%u NS STATUS ", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 348 | |
| 349 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 350 | |
| 351 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
| 352 | DEBUGPC(DGPRS, "missing cause IE\n"); |
| 353 | return -EINVAL; |
| 354 | } |
| 355 | |
| 356 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
| 357 | DEBUGPC(DGPRS, "cause=%s\n", gprs_ns_cause_str(cause)); |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | /* Section 7.3 */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 363 | 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] | 364 | { |
| 365 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 366 | struct tlv_parsed tp; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 367 | uint8_t *cause; |
| 368 | uint16_t *nsvci, *nsei; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 369 | int rc; |
| 370 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 371 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, msgb_l2len(msg), 0, 0); |
| 372 | |
| 373 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 374 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 375 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
| 376 | /* FIXME: respond with NS_CAUSE_MISSING_ESSENT_IE */ |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 377 | LOGP(DGPRS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 378 | return -EINVAL; |
| 379 | } |
| 380 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 381 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 382 | nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 383 | nsei = (uint16_t *) TLVP_VAL(&tp, NS_IE_NSEI); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 384 | |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 385 | DEBUGP(DGPRS, "NSEI=%u NS RESET (NSVCI=%u, cause=%s)\n", |
| 386 | nsvc->nsvci, nsvc->nsei, gprs_ns_cause_str(*cause)); |
| 387 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 388 | nsvc->state = NSE_S_BLOCKED | NSE_S_ALIVE; |
| 389 | nsvc->nsei = ntohs(*nsei); |
| 390 | nsvc->nsvci = ntohs(*nsvci); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 391 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 392 | /* mark the NS-VC as blocked and alive */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 393 | /* start the test procedure */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 394 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 395 | |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 396 | return gprs_ns_tx_reset_ack(nsvc); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* main entry point, here incoming NS frames enter */ |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 400 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 401 | struct sockaddr_in *saddr) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 402 | { |
| 403 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 404 | struct gprs_nsvc *nsvc; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 405 | int rc = 0; |
| 406 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 407 | /* look up the NSVC based on source address */ |
| 408 | nsvc = nsvc_by_rem_addr(nsi, saddr); |
| 409 | if (!nsvc) { |
| 410 | /* Only the RESET procedure creates a new NSVC */ |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 411 | if (nsh->pdu_type != NS_PDUT_RESET) { |
| 412 | LOGP(DGPRS, LOGL_INFO, "Ignoring NS PDU type 0x%0x " |
| 413 | "from %s for non-existing NS-VC\n", |
| 414 | nsh->pdu_type, inet_ntoa(saddr->sin_addr)); |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame^] | 415 | //gprs_ns_tx_reset(nsvc, NS_CAUSE_NSVC_UNKNOWN); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 416 | return -EIO; |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 417 | } |
| 418 | LOGP(DGPRS, LOGL_INFO, "Creating NS-VC for BSS at %s:%u\n", |
| 419 | inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port)); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 420 | nsvc = nsvc_create(nsi, 0xffff); |
| 421 | nsvc->ip.bts_addr = *saddr; |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 422 | } else |
| 423 | msgb_nsei(msg) = nsvc->nsei; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 424 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 425 | switch (nsh->pdu_type) { |
| 426 | case NS_PDUT_ALIVE: |
| 427 | /* remote end inquires whether we're still alive, |
| 428 | * we need to respond with ALIVE_ACK */ |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 429 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 430 | break; |
| 431 | case NS_PDUT_ALIVE_ACK: |
| 432 | /* stop Tns-alive */ |
Harald Welte | 881c205 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 433 | bsc_del_timer(&nsvc->timer); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 434 | /* start Tns-test */ |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 435 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 436 | break; |
| 437 | case NS_PDUT_UNITDATA: |
| 438 | /* actual user data */ |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 439 | rc = gprs_ns_rx_unitdata(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 440 | break; |
| 441 | case NS_PDUT_STATUS: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 442 | rc = gprs_ns_rx_status(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 443 | break; |
| 444 | case NS_PDUT_RESET: |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 445 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 446 | break; |
| 447 | case NS_PDUT_RESET_ACK: |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 448 | DEBUGP(DGPRS, "NSEI=%u Rx NS RESET ACK\n", nsvc->nsei); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 449 | /* mark remote NS-VC as blocked + active */ |
| 450 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 451 | if (nsvc->remote_end_is_sgsn) { |
| 452 | /* stop RESET timer */ |
| 453 | bsc_del_timer(&nsvc->timer); |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame^] | 454 | /* send ALIVE PDU */ |
| 455 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 456 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 457 | } |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 458 | break; |
| 459 | case NS_PDUT_UNBLOCK: |
| 460 | /* Section 7.2: unblocking procedure */ |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 461 | DEBUGP(DGPRS, "NSEI=%u Rx NS UNBLOCK\n", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 462 | nsvc->state &= ~NSE_S_BLOCKED; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 463 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 464 | break; |
| 465 | case NS_PDUT_UNBLOCK_ACK: |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 466 | DEBUGP(DGPRS, "NSEI=%u Rx NS UNBLOCK ACK\n", nsvc->nsei); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 467 | /* mark remote NS-VC as unblocked + active */ |
| 468 | nsvc->remote_state = NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 469 | break; |
| 470 | case NS_PDUT_BLOCK: |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 471 | DEBUGP(DGPRS, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 472 | nsvc->state |= NSE_S_BLOCKED; |
Harald Welte | 7491c77 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 473 | rc = gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 474 | break; |
| 475 | case NS_PDUT_BLOCK_ACK: |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 476 | DEBUGP(DGPRS, "NSEI=%u Rx NS BLOCK ACK\n", nsvc->nsei); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 477 | /* mark remote NS-VC as blocked + active */ |
| 478 | nsvc->remote_state = NSE_S_BLOCKED | NSE_S_ALIVE; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 479 | break; |
| 480 | default: |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 481 | DEBUGP(DGPRS, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n", |
| 482 | nsvc->nsei, nsh->pdu_type); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 483 | rc = -EINVAL; |
| 484 | break; |
| 485 | } |
| 486 | return rc; |
| 487 | } |
| 488 | |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 489 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb) |
| 490 | { |
| 491 | struct gprs_ns_inst *nsi = talloc_zero(tall_bsc_ctx, struct gprs_ns_inst); |
| 492 | |
| 493 | nsi->cb = cb; |
| 494 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
| 495 | |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 496 | return nsi; |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 500 | { |
| 501 | /* FIXME: clear all timers */ |
| 502 | |
| 503 | /* recursively free the NSI and all its NSVCs */ |
| 504 | talloc_free(nsi); |
| 505 | } |
| 506 | |
| 507 | |
| 508 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 509 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 510 | |
| 511 | /* Read a single NS-over-IP message */ |
| 512 | static struct msgb *read_nsip_msg(struct bsc_fd *bfd, int *error, |
| 513 | struct sockaddr_in *saddr) |
| 514 | { |
| 515 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Abis/IP/GPRS-NS"); |
| 516 | int ret = 0; |
| 517 | socklen_t saddr_len = sizeof(*saddr); |
| 518 | |
| 519 | if (!msg) { |
| 520 | *error = -ENOMEM; |
| 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 525 | (struct sockaddr *)saddr, &saddr_len); |
| 526 | if (ret < 0) { |
Harald Welte | ebcd1b7 | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 527 | LOGP(DGPRS, LOGL_ERROR, "recv error %s during NSIP recv\n", |
| 528 | strerror(errno)); |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 529 | msgb_free(msg); |
| 530 | *error = ret; |
| 531 | return NULL; |
| 532 | } else if (ret == 0) { |
| 533 | msgb_free(msg); |
| 534 | *error = ret; |
| 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | msg->l2h = msg->data; |
| 539 | msgb_put(msg, ret); |
| 540 | |
| 541 | return msg; |
| 542 | } |
| 543 | |
| 544 | static int handle_nsip_read(struct bsc_fd *bfd) |
| 545 | { |
| 546 | int error; |
| 547 | struct sockaddr_in saddr; |
| 548 | struct gprs_ns_inst *nsi = bfd->data; |
| 549 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 550 | |
| 551 | if (!msg) |
| 552 | return error; |
| 553 | |
| 554 | return gprs_ns_rcvmsg(nsi, msg, &saddr); |
| 555 | } |
| 556 | |
| 557 | static int handle_nsip_write(struct bsc_fd *bfd) |
| 558 | { |
| 559 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 560 | return -EIO; |
| 561 | } |
| 562 | |
Harald Welte | cd788ba | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 563 | int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 564 | { |
| 565 | int rc; |
| 566 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 567 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 568 | |
| 569 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 570 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 571 | |
| 572 | talloc_free(msg); |
| 573 | |
| 574 | return rc; |
| 575 | } |
| 576 | |
| 577 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
| 578 | static int nsip_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 579 | { |
| 580 | int rc = 0; |
| 581 | |
| 582 | if (what & BSC_FD_READ) |
| 583 | rc = handle_nsip_read(bfd); |
| 584 | if (what & BSC_FD_WRITE) |
| 585 | rc = handle_nsip_write(bfd); |
| 586 | |
| 587 | return rc; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | /* FIXME: this is currently in input/ipaccess.c */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 592 | 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] | 593 | int (*cb)(struct bsc_fd *fd, unsigned int what)); |
| 594 | |
| 595 | /* Listen for incoming GPRS packets */ |
| 596 | int nsip_listen(struct gprs_ns_inst *nsi, uint16_t udp_port) |
| 597 | { |
| 598 | int ret; |
| 599 | |
| 600 | ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, udp_port, nsip_fd_cb); |
| 601 | if (ret < 0) |
| 602 | return ret; |
| 603 | |
| 604 | nsi->ll = GPRS_NS_LL_UDP; |
| 605 | nsi->nsip.fd.data = nsi; |
| 606 | |
| 607 | return ret; |
| 608 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 609 | |
| 610 | /* Establish a connection (from the BSS) to the SGSN */ |
| 611 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 612 | struct sockaddr_in *dest, uint16_t nsei, |
| 613 | uint16_t nsvci) |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 614 | { |
| 615 | struct gprs_nsvc *nsvc; |
| 616 | |
| 617 | nsvc = nsvc_by_rem_addr(nsi, dest); |
| 618 | if (!nsvc) { |
| 619 | nsvc = nsvc_create(nsi, nsvci); |
| 620 | nsvc->ip.bts_addr = *dest; |
| 621 | } |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 622 | nsvc->nsei = nsei; |
| 623 | nsvc->nsvci = nsvci; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 624 | nsvc->remote_end_is_sgsn = 1; |
| 625 | |
| 626 | /* Initiate a RESET procedure */ |
Harald Welte | 81926ba | 2010-05-03 21:05:24 +0200 | [diff] [blame^] | 627 | if (gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION) < 0) { |
Harald Welte | ec4c159 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 628 | LOGP(DGPRS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
| 629 | nsei); |
| 630 | } |
| 631 | /* run a timer and re-transmit the reset request? */ |
| 632 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 633 | |
| 634 | return nsvc; |
| 635 | } |