Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 1 | /* GPRS BSSGP protocol implementation as per 3GPP TS 08.18 */ |
| 2 | |
Harald Welte | 605ac5d | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 3 | /* (C) 2009-2012 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 15 | * GNU General Public License for more details. |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 16 | * |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <netinet/in.h> |
| 26 | |
| 27 | #include <osmocom/core/msgb.h> |
| 28 | #include <osmocom/core/rate_ctr.h> |
| 29 | #include <osmocom/gsm/tlv.h> |
| 30 | #include <osmocom/core/talloc.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 31 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | 30fabdf | 2012-09-10 08:54:35 +0200 | [diff] [blame] | 32 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 33 | #include <osmocom/gprs/gprs_ns.h> |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 34 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 35 | #include "common_vty.h" |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 36 | |
Jacob Erlbeck | c1cb75e | 2015-06-18 13:21:30 +0200 | [diff] [blame] | 37 | #define GSM_IMSI_LENGTH 17 |
| 38 | |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 39 | uint8_t *bssgp_msgb_tlli_put(struct msgb *msg, uint32_t tlli) |
| 40 | { |
| 41 | uint32_t _tlli = htonl(tlli); |
| 42 | return msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli); |
| 43 | } |
| 44 | |
| 45 | /*! \brief GMM-SUSPEND.req (Chapter 10.3.6) */ |
| 46 | int bssgp_tx_suspend(uint16_t nsei, uint32_t tlli, |
| 47 | const struct gprs_ra_id *ra_id) |
| 48 | { |
| 49 | struct msgb *msg = bssgp_msgb_alloc(); |
| 50 | struct bssgp_normal_hdr *bgph = |
| 51 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 52 | uint8_t ra[6]; |
| 53 | |
| 54 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx SUSPEND (TLLI=0x%04x)\n", |
| 55 | tlli); |
| 56 | msgb_nsei(msg) = nsei; |
| 57 | msgb_bvci(msg) = 0; /* Signalling */ |
| 58 | bgph->pdu_type = BSSGP_PDUT_SUSPEND; |
| 59 | |
| 60 | bssgp_msgb_tlli_put(msg, tlli); |
| 61 | |
| 62 | gsm48_construct_ra(ra, ra_id); |
| 63 | msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra); |
| 64 | |
| 65 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 66 | } |
| 67 | |
| 68 | /*! \brief GMM-RESUME.req (Chapter 10.3.9) */ |
| 69 | int bssgp_tx_resume(uint16_t nsei, uint32_t tlli, |
| 70 | const struct gprs_ra_id *ra_id, uint8_t suspend_ref) |
| 71 | { |
| 72 | struct msgb *msg = bssgp_msgb_alloc(); |
| 73 | struct bssgp_normal_hdr *bgph = |
| 74 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 75 | uint8_t ra[6]; |
| 76 | |
| 77 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx RESUME (TLLI=0x%04x)\n", |
| 78 | tlli); |
| 79 | msgb_nsei(msg) = nsei; |
| 80 | msgb_bvci(msg) = 0; /* Signalling */ |
| 81 | bgph->pdu_type = BSSGP_PDUT_RESUME; |
| 82 | |
| 83 | bssgp_msgb_tlli_put(msg, tlli); |
| 84 | |
| 85 | gsm48_construct_ra(ra, ra_id); |
| 86 | msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra); |
| 87 | |
| 88 | msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref); |
| 89 | |
| 90 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 91 | } |
| 92 | |
| 93 | /*! \brief Transmit RA-CAPABILITY-UPDATE (10.3.3) */ |
| 94 | int bssgp_tx_ra_capa_upd(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag) |
| 95 | { |
| 96 | struct msgb *msg = bssgp_msgb_alloc(); |
| 97 | struct bssgp_normal_hdr *bgph = |
| 98 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 99 | |
| 100 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RA-CAPA-UPD (TLLI=0x%04x)\n", |
| 101 | bctx->bvci, tlli); |
| 102 | |
| 103 | /* set NSEI and BVCI in msgb cb */ |
| 104 | msgb_nsei(msg) = bctx->nsei; |
| 105 | msgb_bvci(msg) = bctx->bvci; |
| 106 | |
| 107 | bgph->pdu_type = BSSGP_PDUT_RA_CAPA_UDPATE; |
| 108 | bssgp_msgb_tlli_put(msg, tlli); |
| 109 | |
| 110 | msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag); |
| 111 | |
| 112 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 113 | } |
| 114 | |
| 115 | /* first common part of RADIO-STATUS */ |
| 116 | static struct msgb *common_tx_radio_status(struct bssgp_bvc_ctx *bctx) |
| 117 | { |
| 118 | struct msgb *msg = bssgp_msgb_alloc(); |
| 119 | struct bssgp_normal_hdr *bgph = |
| 120 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 121 | |
| 122 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RADIO-STATUS ", |
| 123 | bctx->bvci); |
| 124 | |
| 125 | /* set NSEI and BVCI in msgb cb */ |
| 126 | msgb_nsei(msg) = bctx->nsei; |
| 127 | msgb_bvci(msg) = bctx->bvci; |
| 128 | |
| 129 | bgph->pdu_type = BSSGP_PDUT_RADIO_STATUS; |
| 130 | |
| 131 | return msg; |
| 132 | } |
| 133 | |
| 134 | /* second common part of RADIO-STATUS */ |
| 135 | static int common_tx_radio_status2(struct msgb *msg, uint8_t cause) |
| 136 | { |
| 137 | msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause); |
| 138 | LOGPC(DBSSGP, LOGL_NOTICE, "CAUSE=%u\n", cause); |
| 139 | |
| 140 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 141 | } |
| 142 | |
| 143 | /*! \brief Transmit RADIO-STATUS for TLLI (10.3.5) */ |
| 144 | int bssgp_tx_radio_status_tlli(struct bssgp_bvc_ctx *bctx, uint8_t cause, |
| 145 | uint32_t tlli) |
| 146 | { |
| 147 | struct msgb *msg = common_tx_radio_status(bctx); |
| 148 | |
| 149 | if (!msg) |
| 150 | return -ENOMEM; |
| 151 | bssgp_msgb_tlli_put(msg, tlli); |
| 152 | LOGPC(DBSSGP, LOGL_NOTICE, "TLLI=0x%08x ", tlli); |
| 153 | |
| 154 | return common_tx_radio_status2(msg, cause); |
| 155 | } |
| 156 | |
| 157 | /*! \brief Transmit RADIO-STATUS for TMSI (10.3.5) */ |
| 158 | int bssgp_tx_radio_status_tmsi(struct bssgp_bvc_ctx *bctx, uint8_t cause, |
| 159 | uint32_t tmsi) |
| 160 | { |
| 161 | struct msgb *msg = common_tx_radio_status(bctx); |
| 162 | uint32_t _tmsi = htonl(tmsi); |
| 163 | |
| 164 | if (!msg) |
| 165 | return -ENOMEM; |
| 166 | msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *)&_tmsi); |
| 167 | LOGPC(DBSSGP, LOGL_NOTICE, "TMSI=0x%08x ", tmsi); |
| 168 | |
| 169 | return common_tx_radio_status2(msg, cause); |
| 170 | } |
| 171 | |
| 172 | /*! \brief Transmit RADIO-STATUS for IMSI (10.3.5) */ |
| 173 | int bssgp_tx_radio_status_imsi(struct bssgp_bvc_ctx *bctx, uint8_t cause, |
| 174 | const char *imsi) |
| 175 | { |
| 176 | struct msgb *msg = common_tx_radio_status(bctx); |
| 177 | uint8_t mi[10]; |
| 178 | int imsi_len = gsm48_generate_mid_from_imsi(mi, imsi); |
| 179 | |
| 180 | if (!msg) |
| 181 | return -ENOMEM; |
| 182 | |
| 183 | /* strip the MI type and length values (2 bytes) */ |
| 184 | if (imsi_len > 2) |
| 185 | msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2); |
| 186 | LOGPC(DBSSGP, LOGL_NOTICE, "IMSI=%s ", imsi); |
| 187 | |
| 188 | return common_tx_radio_status2(msg, cause); |
| 189 | } |
| 190 | |
| 191 | /*! \brief Transmit FLUSH-LL-ACK (Chapter 10.4.2) */ |
| 192 | int bssgp_tx_flush_ll_ack(struct bssgp_bvc_ctx *bctx, uint32_t tlli, |
| 193 | uint8_t action, uint16_t bvci_new, |
| 194 | uint32_t num_octets) |
| 195 | { |
| 196 | struct msgb *msg = bssgp_msgb_alloc(); |
| 197 | struct bssgp_normal_hdr *bgph = |
| 198 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 199 | uint16_t _bvci_new = htons(bvci_new); |
| 200 | uint32_t _oct_aff = htonl(num_octets & 0xFFFFFF); |
| 201 | |
| 202 | msgb_nsei(msg) = bctx->nsei; |
| 203 | msgb_bvci(msg) = 0; /* Signalling */ |
| 204 | bgph->pdu_type = BSSGP_PDUT_FLUSH_LL_ACK; |
| 205 | |
| 206 | bssgp_msgb_tlli_put(msg, tlli); |
| 207 | msgb_tvlv_put(msg, BSSGP_IE_FLUSH_ACTION, 1, &action); |
| 208 | if (action == 1) /* transferred */ |
| 209 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci_new); |
| 210 | msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, (uint8_t *) &_oct_aff); |
| 211 | |
| 212 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 213 | } |
| 214 | |
| 215 | /*! \brief Transmit LLC-DISCARDED (Chapter 10.4.3) */ |
| 216 | int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli, |
| 217 | uint8_t num_frames, uint32_t num_octets) |
| 218 | { |
| 219 | struct msgb *msg = bssgp_msgb_alloc(); |
| 220 | struct bssgp_normal_hdr *bgph = |
| 221 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 222 | uint16_t _bvci = htons(bctx->bvci); |
| 223 | uint32_t _oct_aff = htonl(num_octets & 0xFFFFFF); |
| 224 | |
| 225 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx LLC-DISCARDED " |
| 226 | "TLLI=0x%04x, FRAMES=%u, OCTETS=%u\n", bctx->bvci, tlli, |
| 227 | num_frames, num_octets); |
| 228 | msgb_nsei(msg) = bctx->nsei; |
| 229 | msgb_bvci(msg) = 0; /* Signalling */ |
| 230 | bgph->pdu_type = BSSGP_PDUT_LLC_DISCARD; |
| 231 | |
| 232 | bssgp_msgb_tlli_put(msg, tlli); |
| 233 | |
| 234 | msgb_tvlv_put(msg, BSSGP_IE_LLC_FRAMES_DISCARDED, 1, &num_frames); |
| 235 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
Andreas Eversberg | aa5d0e8 | 2012-07-21 13:33:39 +0200 | [diff] [blame] | 236 | msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, ((uint8_t *) &_oct_aff) + 1); |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 237 | |
| 238 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 239 | } |
| 240 | |
| 241 | /*! \brief Transmit a BVC-BLOCK message (Chapter 10.4.8) */ |
| 242 | int bssgp_tx_bvc_block(struct bssgp_bvc_ctx *bctx, uint8_t cause) |
| 243 | { |
| 244 | struct msgb *msg = bssgp_msgb_alloc(); |
| 245 | struct bssgp_normal_hdr *bgph = |
| 246 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 247 | uint16_t _bvci = htons(bctx->bvci); |
| 248 | |
| 249 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK " |
| 250 | "CAUSE=%u\n", bctx->bvci, cause); |
| 251 | |
| 252 | msgb_nsei(msg) = bctx->nsei; |
| 253 | msgb_bvci(msg) = 0; /* Signalling */ |
| 254 | bgph->pdu_type = BSSGP_PDUT_BVC_BLOCK; |
| 255 | |
| 256 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
| 257 | msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause); |
| 258 | |
| 259 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 260 | } |
| 261 | |
| 262 | /*! \brief Transmit a BVC-UNBLOCK message (Chapter 10.4.10) */ |
| 263 | int bssgp_tx_bvc_unblock(struct bssgp_bvc_ctx *bctx) |
| 264 | { |
| 265 | struct msgb *msg = bssgp_msgb_alloc(); |
| 266 | struct bssgp_normal_hdr *bgph = |
| 267 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 268 | uint16_t _bvci = htons(bctx->bvci); |
| 269 | |
| 270 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK\n", bctx->bvci); |
| 271 | |
| 272 | msgb_nsei(msg) = bctx->nsei; |
| 273 | msgb_bvci(msg) = 0; /* Signalling */ |
| 274 | bgph->pdu_type = BSSGP_PDUT_BVC_UNBLOCK; |
| 275 | |
| 276 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
| 277 | |
| 278 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 279 | } |
| 280 | |
| 281 | /*! \brief Transmit a BVC-RESET message (Chapter 10.4.12) */ |
| 282 | int bssgp_tx_bvc_reset(struct bssgp_bvc_ctx *bctx, uint16_t bvci, uint8_t cause) |
| 283 | { |
| 284 | struct msgb *msg = bssgp_msgb_alloc(); |
| 285 | struct bssgp_normal_hdr *bgph = |
| 286 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 287 | uint16_t _bvci = htons(bvci); |
| 288 | |
| 289 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET " |
| 290 | "CAUSE=%u\n", bvci, cause); |
| 291 | |
| 292 | msgb_nsei(msg) = bctx->nsei; |
| 293 | msgb_bvci(msg) = 0; /* Signalling */ |
| 294 | bgph->pdu_type = BSSGP_PDUT_BVC_RESET; |
| 295 | |
| 296 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
| 297 | msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause); |
| 298 | if (bvci != BVCI_PTM) { |
| 299 | uint8_t bssgp_cid[8]; |
| 300 | bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id); |
| 301 | msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid); |
| 302 | } |
| 303 | /* Optional: Feature Bitmap */ |
| 304 | |
| 305 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 306 | } |
| 307 | |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 308 | /*! \brief Transmit a FLOW_CONTROL-BVC (Chapter 10.4.4) |
| 309 | * \param[in] bctx BVC Context |
Andreas Eversberg | b8d18f3 | 2012-09-30 16:59:33 +0200 | [diff] [blame] | 310 | * \param[in] tag Additional tag to identify acknowledge |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 311 | * \param[in] bucket_size Maximum bucket size in octets |
| 312 | * \param[in] bucket_leak_rate Bucket leak rate in octets/sec |
| 313 | * \param[in] bmax_default_ms Maximum bucket size default for MS |
| 314 | * \param[in] r_default_ms Bucket leak rate default for MS in octets/sec |
| 315 | * \param[in] bucket_full_ratio Ratio (in percent) of queue filling |
| 316 | * \param[in] queue_delay_ms Average queuing delay in milliseconds |
| 317 | */ |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 318 | int bssgp_tx_fc_bvc(struct bssgp_bvc_ctx *bctx, uint8_t tag, |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 319 | uint32_t bucket_size, uint32_t bucket_leak_rate, |
Jacob Erlbeck | 9385d1e | 2015-05-06 09:29:32 +0200 | [diff] [blame] | 320 | uint32_t bmax_default_ms, uint32_t r_default_ms, |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 321 | uint8_t *bucket_full_ratio, uint32_t *queue_delay_ms) |
| 322 | { |
| 323 | struct msgb *msg; |
| 324 | struct bssgp_normal_hdr *bgph; |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 325 | uint16_t e_bucket_size, e_leak_rate, e_bmax_default_ms, e_r_default_ms; |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 326 | uint16_t e_queue_delay = 0; /* to make gcc happy */ |
| 327 | |
| 328 | if ((bucket_size / 100) > 0xffff) |
| 329 | return -EINVAL; |
Jacob Erlbeck | 580af4b | 2015-04-29 15:00:30 +0200 | [diff] [blame] | 330 | e_bucket_size = htons(bucket_size / 100); |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 331 | |
| 332 | if ((bucket_leak_rate * 8 / 100) > 0xffff) |
| 333 | return -EINVAL; |
Jacob Erlbeck | 580af4b | 2015-04-29 15:00:30 +0200 | [diff] [blame] | 334 | e_leak_rate = htons((bucket_leak_rate * 8) / 100); |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 335 | |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 336 | if ((bmax_default_ms / 100) > 0xffff) |
| 337 | return -EINVAL; |
Jacob Erlbeck | 580af4b | 2015-04-29 15:00:30 +0200 | [diff] [blame] | 338 | e_bmax_default_ms = htons(bmax_default_ms / 100); |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 339 | |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 340 | if ((r_default_ms * 8 / 100) > 0xffff) |
| 341 | return -EINVAL; |
Jacob Erlbeck | 580af4b | 2015-04-29 15:00:30 +0200 | [diff] [blame] | 342 | e_r_default_ms = htons((r_default_ms * 8) / 100); |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 343 | |
| 344 | if (queue_delay_ms) { |
| 345 | if ((*queue_delay_ms / 10) > 60000) |
| 346 | return -EINVAL; |
| 347 | else if (*queue_delay_ms == 0xFFFFFFFF) |
| 348 | e_queue_delay = 0xFFFF; |
| 349 | else |
Jacob Erlbeck | 580af4b | 2015-04-29 15:00:30 +0200 | [diff] [blame] | 350 | e_queue_delay = htons(*queue_delay_ms / 10); |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | msg = bssgp_msgb_alloc(); |
| 354 | bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 355 | msgb_nsei(msg) = bctx->nsei; |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 356 | msgb_bvci(msg) = bctx->bvci; |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 357 | bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC; |
| 358 | |
| 359 | msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag); |
| 360 | msgb_tvlv_put(msg, BSSGP_IE_BVC_BUCKET_SIZE, |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 361 | sizeof(e_bucket_size), (uint8_t *) &e_bucket_size); |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 362 | msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE, |
| 363 | sizeof(e_leak_rate), (uint8_t *) &e_leak_rate); |
| 364 | msgb_tvlv_put(msg, BSSGP_IE_BMAX_DEFAULT_MS, |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 365 | sizeof(e_bmax_default_ms), |
| 366 | (uint8_t *) &e_bmax_default_ms); |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 367 | msgb_tvlv_put(msg, BSSGP_IE_R_DEFAULT_MS, |
| 368 | sizeof(e_r_default_ms), (uint8_t *) &e_r_default_ms); |
| 369 | if (bucket_full_ratio) |
| 370 | msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO, |
| 371 | 1, bucket_full_ratio); |
| 372 | if (queue_delay_ms) |
| 373 | msgb_tvlv_put(msg, BSSGP_IE_BVC_MEASUREMENT, |
| 374 | sizeof(e_queue_delay), |
| 375 | (uint8_t *) &e_queue_delay); |
| 376 | |
| 377 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 378 | } |
| 379 | |
| 380 | /*! \brief Transmit a FLOW_CONTROL-MS (Chapter 10.4.6) |
| 381 | * \param[in] bctx BVC Context |
Andreas Eversberg | b8d18f3 | 2012-09-30 16:59:33 +0200 | [diff] [blame] | 382 | * \param[in] tlli TLLI to identify MS |
| 383 | * \param[in] tag Additional tag to identify acknowledge |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 384 | * \param[in] ms_bucket_size Maximum bucket size in octets |
| 385 | * \param[in] bucket_leak_rate Bucket leak rate in octets/sec |
| 386 | * \param[in] bucket_full_ratio Ratio (in percent) of queue filling |
| 387 | */ |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 388 | int bssgp_tx_fc_ms(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag, |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 389 | uint32_t ms_bucket_size, uint32_t bucket_leak_rate, |
| 390 | uint8_t *bucket_full_ratio) |
| 391 | { |
| 392 | struct msgb *msg; |
| 393 | struct bssgp_normal_hdr *bgph; |
| 394 | uint16_t e_bucket_size, e_leak_rate; |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 395 | uint32_t e_tlli; |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 396 | |
| 397 | if ((ms_bucket_size / 100) > 0xffff) |
| 398 | return -EINVAL; |
| 399 | e_bucket_size = ms_bucket_size / 100; |
| 400 | |
| 401 | if ((bucket_leak_rate * 8 / 100) > 0xffff) |
| 402 | return -EINVAL; |
| 403 | e_leak_rate = (bucket_leak_rate * 8) / 100; |
| 404 | |
| 405 | msg = bssgp_msgb_alloc(); |
| 406 | bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 407 | msgb_nsei(msg) = bctx->nsei; |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 408 | msgb_bvci(msg) = bctx->bvci; |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 409 | bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_MS; |
| 410 | |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 411 | e_tlli = htonl(tlli); |
| 412 | msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(e_tlli), (uint8_t *)&e_tlli); |
Harald Welte | e92866b | 2012-09-10 08:56:04 +0200 | [diff] [blame] | 413 | msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag); |
| 414 | msgb_tvlv_put(msg, BSSGP_IE_MS_BUCKET_SIZE, |
| 415 | sizeof(e_bucket_size), (uint8_t *) &e_bucket_size); |
| 416 | msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE, |
| 417 | sizeof(e_leak_rate), (uint8_t *) &e_leak_rate); |
| 418 | if (bucket_full_ratio) |
| 419 | msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO, |
| 420 | 1, bucket_full_ratio); |
| 421 | |
| 422 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 423 | } |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 424 | |
Andreas Eversberg | b8d18f3 | 2012-09-30 16:59:33 +0200 | [diff] [blame] | 425 | /*! \brief RL-UL-UNITDATA.req (Chapter 10.2.2) |
| 426 | * \param[in] bctx BVC Context |
| 427 | * \param[in] tlli TLLI to identify MS |
| 428 | * \param[in] qos_profile Pointer to three octests of QoS profile |
| 429 | * \param[in] llc_pdu msgb pointer containing UL Unitdata IE payload |
| 430 | */ |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 431 | int bssgp_tx_ul_ud(struct bssgp_bvc_ctx *bctx, uint32_t tlli, |
| 432 | const uint8_t *qos_profile, struct msgb *llc_pdu) |
| 433 | { |
| 434 | struct msgb *msg = llc_pdu; |
| 435 | uint8_t bssgp_cid[8]; |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 436 | uint8_t bssgp_align[3] = {0, 0, 0}; |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 437 | struct bssgp_ud_hdr *budh; |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 438 | int align = sizeof(*budh); |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 439 | |
| 440 | /* FIXME: Optional LSA Identifier List, PFI */ |
| 441 | |
| 442 | /* Cell Identifier */ |
| 443 | bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id); |
Andreas Eversberg | f44ed8c | 2012-09-23 06:05:20 +0200 | [diff] [blame] | 444 | align += 2; /* add T+L */ |
| 445 | align += sizeof(bssgp_cid); |
| 446 | |
| 447 | /* First push alignment IE */ |
| 448 | align += 2; /* add T+L */ |
| 449 | align = (4 - align) & 3; /* how many octest are required to align? */ |
| 450 | msgb_tvlv_push(msg, BSSGP_IE_ALIGNMENT, align, bssgp_align); |
| 451 | |
| 452 | /* Push other IEs */ |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 453 | msgb_tvlv_push(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid); |
| 454 | |
| 455 | /* User Data Header */ |
| 456 | budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh)); |
| 457 | budh->tlli = htonl(tlli); |
| 458 | memcpy(budh->qos_profile, qos_profile, 3); |
| 459 | budh->pdu_type = BSSGP_PDUT_UL_UNITDATA; |
| 460 | |
| 461 | /* set NSEI and BVCI in msgb cb */ |
| 462 | msgb_nsei(msg) = bctx->nsei; |
| 463 | msgb_bvci(msg) = bctx->bvci; |
| 464 | |
| 465 | rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]); |
| 466 | rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len); |
| 467 | |
| 468 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 469 | } |
| 470 | |
| 471 | /* Parse a single GMM-PAGING.req to a given NSEI/NS-BVCI */ |
Harald Welte | de4599c | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 472 | int bssgp_rx_paging(struct bssgp_paging_info *pinfo, |
| 473 | struct msgb *msg) |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 474 | { |
| 475 | struct bssgp_normal_hdr *bgph = |
| 476 | (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 477 | struct tlv_parsed tp; |
| 478 | uint8_t ra[6]; |
| 479 | int rc, data_len; |
| 480 | |
| 481 | memset(ra, 0, sizeof(ra)); |
| 482 | |
| 483 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 484 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 485 | if (rc < 0) |
| 486 | goto err_mand_ie; |
| 487 | |
| 488 | switch (bgph->pdu_type) { |
| 489 | case BSSGP_PDUT_PAGING_PS: |
| 490 | pinfo->mode = BSSGP_PAGING_PS; |
| 491 | break; |
| 492 | case BSSGP_PDUT_PAGING_CS: |
| 493 | pinfo->mode = BSSGP_PAGING_CS; |
| 494 | break; |
| 495 | default: |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | |
| 499 | /* IMSI */ |
| 500 | if (!TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) |
| 501 | goto err_mand_ie; |
| 502 | if (!pinfo->imsi) |
Jacob Erlbeck | c1cb75e | 2015-06-18 13:21:30 +0200 | [diff] [blame] | 503 | pinfo->imsi = talloc_zero_size(pinfo, GSM_IMSI_LENGTH); |
| 504 | gsm48_mi_to_string(pinfo->imsi, GSM_IMSI_LENGTH, |
Harald Welte | 85fc314 | 2011-11-25 08:58:40 +0100 | [diff] [blame] | 505 | TLVP_VAL(&tp, BSSGP_IE_IMSI), |
| 506 | TLVP_LEN(&tp, BSSGP_IE_IMSI)); |
| 507 | |
| 508 | /* DRX Parameters */ |
| 509 | if (!TLVP_PRESENT(&tp, BSSGP_IE_DRX_PARAMS)) |
| 510 | goto err_mand_ie; |
| 511 | pinfo->drx_params = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_DRX_PARAMS)); |
| 512 | |
| 513 | /* Scope */ |
| 514 | if (TLVP_PRESENT(&tp, BSSGP_IE_BSS_AREA_ID)) { |
| 515 | pinfo->scope = BSSGP_PAGING_BSS_AREA; |
| 516 | } else if (TLVP_PRESENT(&tp, BSSGP_IE_LOCATION_AREA)) { |
| 517 | pinfo->scope = BSSGP_PAGING_LOCATION_AREA; |
| 518 | memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_LOCATION_AREA), |
| 519 | TLVP_LEN(&tp, BSSGP_IE_LOCATION_AREA)); |
| 520 | gsm48_parse_ra(&pinfo->raid, ra); |
| 521 | } else if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) { |
| 522 | pinfo->scope = BSSGP_PAGING_ROUTEING_AREA; |
| 523 | memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 524 | TLVP_LEN(&tp, BSSGP_IE_ROUTEING_AREA)); |
| 525 | gsm48_parse_ra(&pinfo->raid, ra); |
| 526 | } else if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) { |
| 527 | pinfo->scope = BSSGP_PAGING_BVCI; |
| 528 | pinfo->bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI)); |
| 529 | } else |
| 530 | return -EINVAL; |
| 531 | |
| 532 | /* QoS profile mandatory for PS */ |
| 533 | if (pinfo->mode == BSSGP_PAGING_PS) { |
| 534 | if (!TLVP_PRESENT(&tp, BSSGP_IE_QOS_PROFILE)) |
| 535 | goto err_cond_ie; |
| 536 | if (TLVP_LEN(&tp, BSSGP_IE_QOS_PROFILE) < 3) |
| 537 | goto err; |
| 538 | |
| 539 | memcpy(&pinfo->qos, TLVP_VAL(&tp, BSSGP_IE_QOS_PROFILE), |
| 540 | 3); |
| 541 | } |
| 542 | |
| 543 | /* Optional (P-)TMSI */ |
| 544 | if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI) && |
| 545 | TLVP_LEN(&tp, BSSGP_IE_TMSI) >= 4) |
| 546 | if (!pinfo->ptmsi) |
| 547 | pinfo->ptmsi = talloc_zero_size(pinfo, sizeof(uint32_t)); |
| 548 | *(pinfo->ptmsi) = ntohl(*(uint32_t *) |
| 549 | TLVP_VAL(&tp, BSSGP_IE_TMSI)); |
| 550 | |
| 551 | return 0; |
| 552 | |
| 553 | err_mand_ie: |
| 554 | err_cond_ie: |
| 555 | err: |
| 556 | /* FIXME */ |
| 557 | return 0; |
| 558 | } |