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