Harald Welte | 55d724a | 2017-10-16 18:25:45 +0200 | [diff] [blame] | 1 | /* Point-to-Point (PP) Short Message Service (SMS). |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 2 | * Support on Mobile Radio Interface |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 3 | * 3GPP TS 04.11 version 7.1.0 Release 1998 / ETSI TS 100 942 V7.1.0. */ |
| 4 | /* |
| 5 | * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de> |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 7 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 8 | * (C) 2010 by On-Waves |
| 9 | * (C) 2011 by Andreas Eversberg <jolly@eversberg.eu> |
| 10 | * |
| 11 | * All Rights Reserved |
| 12 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 13 | * SPDX-License-Identifier: GPL-2.0+ |
| 14 | * |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 15 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 388fb03 | 2014-10-26 20:42:49 +0100 | [diff] [blame] | 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 388fb03 | 2014-10-26 20:42:49 +0100 | [diff] [blame] | 23 | * GNU General Public License for more details. |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 24 | * |
Harald Welte | 388fb03 | 2014-10-26 20:42:49 +0100 | [diff] [blame] | 25 | * You should have received a copy of the GNU General Public License |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | /* Notes on msg: |
| 31 | * |
| 32 | * Messages from lower layer are freed by lower layer. |
| 33 | * |
| 34 | * Messages to upper layer are freed after upper layer call returns, so upper |
| 35 | * layer cannot use data after returning. Upper layer must not free the msg. |
| 36 | * |
| 37 | * This implies: Lower layer messages can be forwarded to upper layer. |
| 38 | * |
| 39 | * Upper layer messages are freed by lower layer, so they must not be freed |
| 40 | * after calling lower layer. |
| 41 | * |
| 42 | * |
| 43 | * Notes on release: |
| 44 | * |
| 45 | * Whenever the process returns to IDLE, the MM connection is released using |
| 46 | * MMSMS-REL-REQ. It is allowed to destroy this process while processing |
| 47 | * this message. |
| 48 | * |
Holger Hans Peter Freyther | 1c4c373 | 2012-11-22 00:33:52 +0100 | [diff] [blame] | 49 | * There is an exception, if MMSMS-REL-IND is received from lower layer, the |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 50 | * process returns to IDLE without sending MMSMS-REL-REQ. |
| 51 | * |
| 52 | */ |
| 53 | |
| 54 | #include <string.h> |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 55 | #include <inttypes.h> |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 56 | #include <errno.h> |
| 57 | #include <osmocom/core/msgb.h> |
| 58 | #include <osmocom/core/logging.h> |
| 59 | #include <osmocom/core/timer.h> |
| 60 | |
| 61 | #include <osmocom/gsm/gsm0411_utils.h> |
| 62 | #include <osmocom/gsm/gsm0411_smc.h> |
| 63 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 64 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 65 | /*! \addtogroup sms |
| 66 | * @{ |
Harald Welte | 55d724a | 2017-10-16 18:25:45 +0200 | [diff] [blame] | 67 | * \file gsm0411_smc.c |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 68 | * Point-to-Point (PP) Short Message Service (SMS) as per TS 04.11 |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 69 | */ |
| 70 | |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 71 | static void cp_timer_expired(void *data); |
| 72 | |
Sylvain Munaut | cc90d49 | 2011-11-12 23:52:40 +0100 | [diff] [blame] | 73 | #define MAX_SMS_RETRY 2 |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 74 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 75 | #define SMC_LOG_STR "SMC(%" PRIu64 ") " |
| 76 | |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 77 | /* init a new instance */ |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 78 | void gsm411_smc_init(struct gsm411_smc_inst *inst, uint64_t id, int network, |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 79 | int (*mn_recv) (struct gsm411_smc_inst *inst, int msg_type, |
| 80 | struct msgb *msg), |
| 81 | int (*mm_send) (struct gsm411_smc_inst *inst, int msg_type, |
| 82 | struct msgb *msg, int cp_msg_type)) |
| 83 | { |
| 84 | memset(inst, 0, sizeof(*inst)); |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 85 | inst->id = id; |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 86 | inst->network = network; |
| 87 | inst->cp_max_retr = MAX_SMS_RETRY; |
Sylvain Munaut | 0d9b8ec | 2011-11-12 23:52:20 +0100 | [diff] [blame] | 88 | inst->cp_tc1 = GSM411_TMR_TC1A_SEC / (inst->cp_max_retr + 1); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 89 | inst->cp_state = GSM411_CPS_IDLE; |
| 90 | inst->mn_recv = mn_recv; |
| 91 | inst->mm_send = mm_send; |
| 92 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 93 | LOGP(DLSMS, LOGL_INFO, |
Holger Hans Peter Freyther | 68f9447 | 2012-12-01 12:51:34 +0100 | [diff] [blame] | 94 | SMC_LOG_STR "instance created for %s\n", |
| 95 | inst->id, inst->network ? "network" : "mobile"); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* clear instance */ |
| 99 | void gsm411_smc_clear(struct gsm411_smc_inst *inst) |
| 100 | { |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 101 | LOGP(DLSMS, LOGL_INFO, |
| 102 | SMC_LOG_STR "clearing instance\n", inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 103 | |
| 104 | osmo_timer_del(&inst->cp_timer); |
| 105 | |
| 106 | /* free stored msg */ |
| 107 | if (inst->cp_msg) { |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 108 | LOGP(DLSMS, LOGL_INFO, |
| 109 | SMC_LOG_STR "dropping pending message\n", inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 110 | msgb_free(inst->cp_msg); |
| 111 | inst->cp_msg = NULL; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | const char *smc_state_names[] = { |
| 116 | "IDLE", |
| 117 | "MM_CONN_PENDING", |
| 118 | "WAIT_CP_ACK", |
| 119 | "MM_ESTABLISHED", |
| 120 | }; |
| 121 | |
| 122 | const struct value_string gsm411_cp_cause_strs[] = { |
| 123 | { GSM411_CP_CAUSE_NET_FAIL, "Network Failure" }, |
| 124 | { GSM411_CP_CAUSE_CONGESTION, "Congestion" }, |
| 125 | { GSM411_CP_CAUSE_INV_TRANS_ID, "Invalid Transaction ID" }, |
| 126 | { GSM411_CP_CAUSE_SEMANT_INC_MSG, "Semantically Incorrect Message" }, |
| 127 | { GSM411_CP_CAUSE_INV_MAND_INF, "Invalid Mandatory Information" }, |
| 128 | { GSM411_CP_CAUSE_MSGTYPE_NOTEXIST, "Message Type doesn't exist" }, |
| 129 | { GSM411_CP_CAUSE_MSG_INCOMP_STATE, |
| 130 | "Message incompatible with protocol state" }, |
| 131 | { GSM411_CP_CAUSE_IE_NOTEXIST, "IE does not exist" }, |
| 132 | { GSM411_CP_CAUSE_PROTOCOL_ERR, "Protocol Error" }, |
| 133 | { 0, 0 } |
| 134 | }; |
| 135 | |
| 136 | static void new_cp_state(struct gsm411_smc_inst *inst, |
| 137 | enum gsm411_cp_state state) |
| 138 | { |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 139 | LOGP(DLSMS, LOGL_INFO, |
| 140 | SMC_LOG_STR "new CP state %s -> %s\n", inst->id, |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 141 | smc_state_names[inst->cp_state], smc_state_names[state]); |
| 142 | inst->cp_state = state; |
| 143 | } |
| 144 | |
| 145 | static int gsm411_tx_cp_error(struct gsm411_smc_inst *inst, uint8_t cause) |
| 146 | { |
| 147 | struct msgb *nmsg = gsm411_msgb_alloc(); |
| 148 | uint8_t *causep; |
| 149 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 150 | LOGP(DLSMS, LOGL_NOTICE, |
| 151 | SMC_LOG_STR "TX CP-ERROR, cause %d (%s)\n", inst->id, cause, |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 152 | get_value_string(gsm411_cp_cause_strs, cause)); |
| 153 | |
| 154 | causep = msgb_put(nmsg, 1); |
| 155 | *causep = cause; |
| 156 | |
| 157 | return inst->mm_send(inst, GSM411_MMSMS_DATA_REQ, nmsg, |
| 158 | GSM411_MT_CP_ERROR); |
| 159 | } |
| 160 | |
Holger Hans Peter Freyther | 866fc91 | 2012-11-13 22:29:35 +0100 | [diff] [blame] | 161 | /* establish SMC connection */ |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 162 | static int gsm411_mnsms_est_req(struct gsm411_smc_inst *inst, struct msgb *msg) |
| 163 | { |
| 164 | struct msgb *nmsg; |
| 165 | |
| 166 | if (inst->cp_msg) { |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 167 | LOGP(DLSMS, LOGL_FATAL, |
| 168 | SMC_LOG_STR "EST REQ, but we already have an " |
| 169 | "cp_msg. This should never happen, please fix!\n", |
| 170 | inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 171 | msgb_free(inst->cp_msg); |
| 172 | } |
| 173 | |
| 174 | inst->cp_msg = msg; |
| 175 | new_cp_state(inst, GSM411_CPS_MM_CONN_PENDING); |
| 176 | /* clear stored release flag */ |
| 177 | inst->cp_rel = 0; |
| 178 | /* send MMSMS_EST_REQ */ |
| 179 | nmsg = gsm411_msgb_alloc(); |
| 180 | return inst->mm_send(inst, GSM411_MMSMS_EST_REQ, nmsg, 0); |
| 181 | } |
| 182 | |
| 183 | static int gsm411_mmsms_send_msg(struct gsm411_smc_inst *inst) |
| 184 | { |
| 185 | struct msgb *nmsg; |
| 186 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 187 | LOGP(DLSMS, LOGL_INFO, |
| 188 | SMC_LOG_STR "send CP data\n", inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 189 | /* reset retry counter */ |
| 190 | if (inst->cp_state != GSM411_CPS_WAIT_CP_ACK) |
| 191 | inst->cp_retx = 0; |
| 192 | /* 5.2.3.1.2: enter MO-wait for CP-ACK */ |
| 193 | /* 5.2.3.2.3: enter MT-wait for CP-ACK */ |
| 194 | new_cp_state(inst, GSM411_CPS_WAIT_CP_ACK); |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 195 | osmo_timer_setup(&inst->cp_timer, cp_timer_expired, inst); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 196 | /* 5.3.2.1: Set Timer TC1A */ |
| 197 | osmo_timer_schedule(&inst->cp_timer, inst->cp_tc1, 0); |
| 198 | /* clone cp_msg */ |
| 199 | nmsg = gsm411_msgb_alloc(); |
| 200 | memcpy(msgb_put(nmsg, inst->cp_msg->len), inst->cp_msg->data, |
| 201 | inst->cp_msg->len); |
| 202 | /* send MMSMS_DATA_REQ with CP-DATA */ |
| 203 | return inst->mm_send(inst, GSM411_MMSMS_DATA_REQ, nmsg, |
| 204 | GSM411_MT_CP_DATA); |
| 205 | } |
| 206 | |
| 207 | static int gsm411_mmsms_est_cnf(struct gsm411_smc_inst *inst, struct msgb *msg) |
| 208 | { |
| 209 | if (!inst->cp_msg) { |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 210 | LOGP(DLSMS, LOGL_FATAL, |
| 211 | SMC_LOG_STR "EST CNF, but we have no cp_msg. This " |
| 212 | "should never happen, please fix!\n", |
| 213 | inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 214 | return -EINVAL; |
| 215 | } |
| 216 | |
| 217 | return gsm411_mmsms_send_msg(inst); |
| 218 | } |
| 219 | |
| 220 | /* SMC TC1* is expired */ |
| 221 | static void cp_timer_expired(void *data) |
| 222 | { |
| 223 | struct gsm411_smc_inst *inst = data; |
| 224 | struct msgb *nmsg; |
| 225 | |
| 226 | if (inst->cp_retx == inst->cp_max_retr) { |
| 227 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 228 | LOGP(DLSMS, LOGL_INFO, |
| 229 | SMC_LOG_STR "TC1* timeout, no more retries.\n", |
| 230 | inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 231 | /* 5.3.2.1: enter idle state */ |
| 232 | new_cp_state(inst, GSM411_CPS_IDLE); |
| 233 | /* indicate error */ |
| 234 | nmsg = gsm411_msgb_alloc(); |
| 235 | inst->mn_recv(inst, GSM411_MNSMS_ERROR_IND, nmsg); |
| 236 | msgb_free(nmsg); |
| 237 | /* free pending stored msg */ |
| 238 | if (inst->cp_msg) { |
| 239 | msgb_free(inst->cp_msg); |
| 240 | inst->cp_msg = NULL; |
| 241 | } |
| 242 | /* release MM connection */ |
| 243 | nmsg = gsm411_msgb_alloc(); |
| 244 | inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0); |
| 245 | return; |
| 246 | } |
| 247 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 248 | LOGP(DLSMS, LOGL_INFO, |
| 249 | SMC_LOG_STR "TC1* timeout, retrying...\n", inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 250 | inst->cp_retx++; |
| 251 | gsm411_mmsms_est_cnf(inst, NULL); |
| 252 | } |
| 253 | |
| 254 | static int gsm411_mmsms_cp_ack(struct gsm411_smc_inst *inst, struct msgb *msg) |
| 255 | { |
| 256 | /* free stored msg */ |
| 257 | if (inst->cp_msg) { |
| 258 | msgb_free(inst->cp_msg); |
| 259 | inst->cp_msg = NULL; |
| 260 | } |
| 261 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 262 | LOGP(DLSMS, LOGL_INFO, |
| 263 | SMC_LOG_STR "received CP-ACK\n", inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 264 | /* 5.3.2.1 enter MM Connection established */ |
| 265 | new_cp_state(inst, GSM411_CPS_MM_ESTABLISHED); |
| 266 | /* 5.3.2.1: Reset Timer TC1* */ |
| 267 | osmo_timer_del(&inst->cp_timer); |
| 268 | |
| 269 | /* pending release? */ |
| 270 | if (inst->cp_rel) { |
| 271 | struct msgb *nmsg; |
| 272 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 273 | LOGP(DLSMS, LOGL_INFO, |
| 274 | SMC_LOG_STR "we have pending release.\n", |
| 275 | inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 276 | new_cp_state(inst, GSM411_CPS_IDLE); |
| 277 | /* release MM connection */ |
| 278 | nmsg = gsm411_msgb_alloc(); |
| 279 | return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0); |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int gsm411_mmsms_cp_data(struct gsm411_smc_inst *inst, struct msgb *msg) |
| 286 | { |
| 287 | struct msgb *nmsg; |
| 288 | int mt = GSM411_MNSMS_DATA_IND; |
| 289 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 290 | LOGP(DLSMS, LOGL_INFO, |
| 291 | SMC_LOG_STR "received CP-DATA\n", inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 292 | /* 5.3.1 enter MM Connection established (if idle) */ |
| 293 | if (inst->cp_state == GSM411_CPS_IDLE) { |
| 294 | new_cp_state(inst, GSM411_CPS_MM_ESTABLISHED); |
| 295 | mt = GSM411_MNSMS_EST_IND; |
| 296 | /* clear stored release flag */ |
| 297 | inst->cp_rel = 0; |
| 298 | } |
| 299 | /* send MMSMS_DATA_REQ (CP ACK) */ |
| 300 | nmsg = gsm411_msgb_alloc(); |
| 301 | inst->mm_send(inst, GSM411_MMSMS_DATA_REQ, nmsg, GSM411_MT_CP_ACK); |
| 302 | /* indicate data */ |
| 303 | inst->mn_recv(inst, mt, msg); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* send CP DATA */ |
| 309 | static int gsm411_mnsms_data_req(struct gsm411_smc_inst *inst, struct msgb *msg) |
| 310 | { |
| 311 | if (inst->cp_msg) { |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 312 | LOGP(DLSMS, LOGL_FATAL, |
| 313 | SMC_LOG_STR "DATA REQ, but we already have an " |
| 314 | "cp_msg. This should never happen, please fix!\n", |
| 315 | inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 316 | msgb_free(inst->cp_msg); |
| 317 | } |
| 318 | |
| 319 | /* store and send */ |
| 320 | inst->cp_msg = msg; |
| 321 | return gsm411_mmsms_send_msg(inst); |
| 322 | } |
| 323 | |
| 324 | /* release SMC connection */ |
| 325 | static int gsm411_mnsms_rel_req(struct gsm411_smc_inst *inst, struct msgb *msg) |
| 326 | { |
| 327 | struct msgb *nmsg; |
| 328 | |
| 329 | msgb_free(msg); |
| 330 | |
| 331 | /* discard silently */ |
| 332 | if (inst->cp_state == GSM411_CPS_IDLE) |
| 333 | return 0; |
| 334 | |
| 335 | /* store release, until established or released */ |
| 336 | if (inst->cp_state != GSM411_CPS_MM_ESTABLISHED) { |
Holger Hans Peter Freyther | 0916159 | 2012-11-11 10:09:24 +0100 | [diff] [blame] | 337 | LOGP(DLSMS, LOGL_NOTICE, |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 338 | SMC_LOG_STR "cannot release yet current state: %s\n", |
| 339 | inst->id, smc_state_names[inst->cp_state]); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 340 | inst->cp_rel = 1; |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /* free stored msg */ |
| 345 | if (inst->cp_msg) { |
| 346 | msgb_free(inst->cp_msg); |
| 347 | inst->cp_msg = NULL; |
| 348 | } |
| 349 | |
| 350 | new_cp_state(inst, GSM411_CPS_IDLE); |
| 351 | /* release MM connection */ |
| 352 | nmsg = gsm411_msgb_alloc(); |
| 353 | return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0); |
| 354 | } |
| 355 | |
| 356 | static int gsm411_mmsms_cp_error(struct gsm411_smc_inst *inst, struct msgb *msg) |
| 357 | { |
| 358 | struct msgb *nmsg; |
| 359 | |
| 360 | /* free stored msg */ |
| 361 | if (inst->cp_msg) { |
| 362 | msgb_free(inst->cp_msg); |
| 363 | inst->cp_msg = NULL; |
| 364 | } |
| 365 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 366 | LOGP(DLSMS, LOGL_INFO, |
| 367 | SMC_LOG_STR "received CP-ERROR\n", inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 368 | /* 5.3.4 enter idle */ |
| 369 | new_cp_state(inst, GSM411_CPS_IDLE); |
| 370 | /* indicate error */ |
| 371 | inst->mn_recv(inst, GSM411_MNSMS_ERROR_IND, msg); |
| 372 | /* release MM connection */ |
| 373 | nmsg = gsm411_msgb_alloc(); |
| 374 | return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0); |
| 375 | } |
| 376 | |
| 377 | static int gsm411_mmsms_rel_ind(struct gsm411_smc_inst *inst, struct msgb *msg) |
| 378 | { |
| 379 | struct msgb *nmsg; |
| 380 | |
| 381 | /* free stored msg */ |
| 382 | if (inst->cp_msg) { |
| 383 | msgb_free(inst->cp_msg); |
| 384 | inst->cp_msg = NULL; |
| 385 | } |
| 386 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 387 | LOGP(DLSMS, LOGL_INFO, |
| 388 | SMC_LOG_STR "MM layer is released\n", inst->id); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 389 | /* 5.3.4 enter idle */ |
| 390 | new_cp_state(inst, GSM411_CPS_IDLE); |
| 391 | /* indicate error */ |
| 392 | nmsg = gsm411_msgb_alloc(); |
| 393 | inst->mn_recv(inst, GSM411_MNSMS_ERROR_IND, nmsg); |
| 394 | msgb_free(nmsg); |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /* abort SMC connection */ |
| 400 | static int gsm411_mnsms_abort_req(struct gsm411_smc_inst *inst, |
| 401 | struct msgb *msg) |
| 402 | { |
| 403 | struct msgb *nmsg; |
| 404 | |
| 405 | /* free stored msg */ |
| 406 | if (inst->cp_msg) { |
| 407 | msgb_free(inst->cp_msg); |
| 408 | inst->cp_msg = NULL; |
| 409 | } |
| 410 | |
| 411 | /* 5.3.4 go idle */ |
| 412 | new_cp_state(inst, GSM411_CPS_IDLE); |
| 413 | /* send MMSMS_DATA_REQ with CP-ERROR */ |
| 414 | inst->mm_send(inst, GSM411_MMSMS_DATA_REQ, msg, GSM411_MT_CP_ERROR); |
| 415 | /* release MM connection */ |
| 416 | nmsg = gsm411_msgb_alloc(); |
| 417 | return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, 0); |
| 418 | } |
| 419 | |
| 420 | /* statefull handling for MNSMS SAP messages */ |
Holger Hans Peter Freyther | 9473c5d | 2012-11-22 10:50:52 +0100 | [diff] [blame] | 421 | static const struct smcdownstate { |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 422 | uint32_t states; |
| 423 | int type; |
| 424 | const char *name; |
| 425 | int (*rout) (struct gsm411_smc_inst *inst, |
| 426 | struct msgb *msg); |
| 427 | } smcdownstatelist[] = { |
| 428 | /* establish request */ |
| 429 | {SBIT(GSM411_CPS_IDLE), |
| 430 | GSM411_MNSMS_EST_REQ, |
| 431 | "MNSMS-EST-REQ", gsm411_mnsms_est_req}, |
| 432 | |
| 433 | /* release request */ |
| 434 | {ALL_STATES, |
| 435 | GSM411_MNSMS_REL_REQ, |
| 436 | "MNSMS-REL-REQ", gsm411_mnsms_rel_req}, |
| 437 | |
| 438 | /* data request */ |
| 439 | {SBIT(GSM411_CPS_MM_ESTABLISHED), |
| 440 | GSM411_MNSMS_DATA_REQ, |
| 441 | "MNSMS-DATA-REQ", gsm411_mnsms_data_req}, |
| 442 | |
| 443 | /* abort request */ |
| 444 | {ALL_STATES - SBIT(GSM411_CPS_IDLE), |
| 445 | GSM411_MNSMS_ABORT_REQ, |
| 446 | "MNSMS-ABORT-REQ", gsm411_mnsms_abort_req}, |
| 447 | }; |
| 448 | |
| 449 | #define SMCDOWNSLLEN \ |
| 450 | (sizeof(smcdownstatelist) / sizeof(struct smcdownstate)) |
| 451 | |
| 452 | /* message from upper layer */ |
| 453 | int gsm411_smc_send(struct gsm411_smc_inst *inst, int msg_type, |
| 454 | struct msgb *msg) |
| 455 | { |
| 456 | int i, rc; |
| 457 | |
| 458 | /* find function for current state and message */ |
| 459 | for (i = 0; i < SMCDOWNSLLEN; i++) { |
| 460 | if ((msg_type == smcdownstatelist[i].type) |
| 461 | && (SBIT(inst->cp_state) & smcdownstatelist[i].states)) |
| 462 | break; |
| 463 | } |
| 464 | if (i == SMCDOWNSLLEN) { |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 465 | LOGP(DLSMS, LOGL_NOTICE, |
| 466 | SMC_LOG_STR "message %u unhandled at this state %s.\n", |
| 467 | inst->id, msg_type, smc_state_names[inst->cp_state]); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 468 | msgb_free(msg); |
| 469 | return 0; |
| 470 | } |
| 471 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 472 | LOGP(DLSMS, LOGL_INFO, |
| 473 | SMC_LOG_STR "message %s received in state %s\n", inst->id, |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 474 | smcdownstatelist[i].name, smc_state_names[inst->cp_state]); |
| 475 | |
| 476 | rc = smcdownstatelist[i].rout(inst, msg); |
| 477 | |
| 478 | return rc; |
| 479 | } |
| 480 | |
| 481 | /* statefull handling for MMSMS SAP messages */ |
Holger Hans Peter Freyther | 9473c5d | 2012-11-22 10:50:52 +0100 | [diff] [blame] | 482 | static const struct smcdatastate { |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 483 | uint32_t states; |
| 484 | int type, cp_type; |
| 485 | const char *name; |
| 486 | int (*rout) (struct gsm411_smc_inst *inst, |
| 487 | struct msgb *msg); |
| 488 | } smcdatastatelist[] = { |
| 489 | /* establish confirm */ |
| 490 | {SBIT(GSM411_CPS_MM_CONN_PENDING), |
| 491 | GSM411_MMSMS_EST_CNF, 0, |
| 492 | "MMSMS-EST-CNF", gsm411_mmsms_est_cnf}, |
| 493 | |
| 494 | /* establish indication (CP DATA) */ |
| 495 | {SBIT(GSM411_CPS_IDLE), |
| 496 | GSM411_MMSMS_EST_IND, GSM411_MT_CP_DATA, |
| 497 | "MMSMS-EST-IND (CP DATA)", gsm411_mmsms_cp_data}, |
| 498 | |
| 499 | /* data indication (CP DATA) */ |
| 500 | {SBIT(GSM411_CPS_MM_ESTABLISHED), |
| 501 | GSM411_MMSMS_DATA_IND, GSM411_MT_CP_DATA, |
| 502 | "MMSMS-DATA-IND (CP DATA)", gsm411_mmsms_cp_data}, |
| 503 | |
| 504 | /* data indication (CP ACK) */ |
| 505 | {SBIT(GSM411_CPS_WAIT_CP_ACK), |
| 506 | GSM411_MMSMS_DATA_IND, GSM411_MT_CP_ACK, |
| 507 | "MMSMS-DATA-IND (CP ACK)", gsm411_mmsms_cp_ack}, |
| 508 | |
| 509 | /* data indication (CP ERROR) */ |
| 510 | {ALL_STATES, |
| 511 | GSM411_MMSMS_DATA_IND, GSM411_MT_CP_ERROR, |
| 512 | "MMSMS-DATA-IND (CP_ERROR)", gsm411_mmsms_cp_error}, |
| 513 | |
| 514 | /* release indication */ |
| 515 | {ALL_STATES - SBIT(GSM411_CPS_IDLE), |
| 516 | GSM411_MMSMS_REL_IND, 0, |
| 517 | "MMSMS-REL-IND", gsm411_mmsms_rel_ind}, |
| 518 | |
| 519 | }; |
| 520 | |
| 521 | #define SMCDATASLLEN \ |
| 522 | (sizeof(smcdatastatelist) / sizeof(struct smcdatastate)) |
| 523 | |
| 524 | /* message from lower layer |
| 525 | * WARNING: We must not free msg, since it will be performed by the |
| 526 | * lower layer. */ |
| 527 | int gsm411_smc_recv(struct gsm411_smc_inst *inst, int msg_type, |
| 528 | struct msgb *msg, int cp_msg_type) |
| 529 | { |
| 530 | int i, rc; |
| 531 | |
| 532 | /* find function for current state and message */ |
| 533 | for (i = 0; i < SMCDATASLLEN; i++) { |
Holger Hans Peter Freyther | 1c4c373 | 2012-11-22 00:33:52 +0100 | [diff] [blame] | 534 | /* state must match, MM message must match |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 535 | * CP msg must match only in case of MMSMS_DATA_IND |
| 536 | */ |
| 537 | if ((msg_type == smcdatastatelist[i].type) |
| 538 | && (SBIT(inst->cp_state) & smcdatastatelist[i].states) |
| 539 | && (msg_type != GSM411_MMSMS_DATA_IND |
| 540 | || cp_msg_type == smcdatastatelist[i].cp_type)) |
| 541 | break; |
| 542 | } |
| 543 | if (i == SMCDATASLLEN) { |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 544 | LOGP(DLSMS, LOGL_NOTICE, |
| 545 | SMC_LOG_STR "message 0x%x/%u unhandled at this " |
| 546 | "state %s.\n", inst->id, msg_type, cp_msg_type, |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 547 | smc_state_names[inst->cp_state]); |
| 548 | if (msg_type == GSM411_MMSMS_EST_IND |
| 549 | || msg_type == GSM411_MMSMS_DATA_IND) { |
| 550 | struct msgb *nmsg; |
| 551 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 552 | LOGP(DLSMS, LOGL_NOTICE, |
| 553 | SMC_LOG_STR "RX Unimplemented CP " |
| 554 | "msg_type: 0x%02x\n", inst->id, msg_type); |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 555 | /* 5.3.4 enter idle */ |
| 556 | new_cp_state(inst, GSM411_CPS_IDLE); |
| 557 | /* indicate error */ |
| 558 | gsm411_tx_cp_error(inst, |
| 559 | GSM411_CP_CAUSE_MSGTYPE_NOTEXIST); |
| 560 | /* send error indication to upper layer */ |
| 561 | nmsg = gsm411_msgb_alloc(); |
| 562 | inst->mn_recv(inst, GSM411_MNSMS_ERROR_IND, nmsg); |
| 563 | msgb_free(nmsg); |
| 564 | /* release MM connection */ |
| 565 | nmsg = gsm411_msgb_alloc(); |
| 566 | return inst->mm_send(inst, GSM411_MMSMS_REL_REQ, nmsg, |
| 567 | 0); |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
Holger Hans Peter Freyther | 33e8a87 | 2012-11-14 06:07:47 +0100 | [diff] [blame] | 572 | LOGP(DLSMS, LOGL_INFO, |
| 573 | SMC_LOG_STR "message %s received in state %s\n", inst->id, |
Andreas Eversberg | bbf9034 | 2011-10-28 03:55:37 +0200 | [diff] [blame] | 574 | smcdatastatelist[i].name, smc_state_names[inst->cp_state]); |
| 575 | |
| 576 | rc = smcdatastatelist[i].rout(inst, msg); |
| 577 | |
| 578 | return rc; |
| 579 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 580 | |
| 581 | /*! @} */ |