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