Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* Common bits for RAN message handling */ |
| 2 | /* |
| 3 | * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Neels Hofmeyr |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/utils.h> |
| 22 | |
| 23 | #include <osmocom/msc/ran_msg.h> |
| 24 | |
| 25 | const struct value_string ran_msg_type_names[] = { |
| 26 | { RAN_MSG_NONE, "NONE" }, |
| 27 | { RAN_MSG_COMPL_L3, "COMPL_L3" }, |
| 28 | { RAN_MSG_DTAP, "DTAP" }, |
| 29 | { RAN_MSG_CLEAR_COMMAND, "CLEAR_COMMAND" }, |
| 30 | { RAN_MSG_CLEAR_REQUEST, "CLEAR_REQUEST" }, |
| 31 | { RAN_MSG_CLEAR_COMPLETE, "CLEAR_COMPLETE" }, |
| 32 | { RAN_MSG_CLASSMARK_REQUEST, "CLASSMARK_REQUEST" }, |
| 33 | { RAN_MSG_CLASSMARK_UPDATE, "CLASSMARK_UPDATE" }, |
| 34 | { RAN_MSG_CIPHER_MODE_COMMAND, "CIPHER_MODE_COMMAND" }, |
| 35 | { RAN_MSG_CIPHER_MODE_COMPLETE, "CIPHER_MODE_COMPLETE" }, |
| 36 | { RAN_MSG_CIPHER_MODE_REJECT, "CIPHER_MODE_REJECT" }, |
| 37 | { RAN_MSG_COMMON_ID, "COMMON_ID" }, |
| 38 | { RAN_MSG_ASSIGNMENT_COMMAND, "ASSIGNMENT_COMMAND" }, |
| 39 | { RAN_MSG_ASSIGNMENT_COMPLETE, "ASSIGNMENT_COMPLETE" }, |
| 40 | { RAN_MSG_ASSIGNMENT_FAILURE, "ASSIGNMENT_FAILURE" }, |
| 41 | { RAN_MSG_SAPI_N_REJECT, "SAPI_N_REJECT" }, |
| 42 | { RAN_MSG_LCLS_STATUS, "LCLS_STATUS" }, |
| 43 | { RAN_MSG_LCLS_BREAK_REQ, "LCLS_BREAK_REQ" }, |
| 44 | { RAN_MSG_HANDOVER_COMMAND, "HANDOVER_COMMAND" }, |
| 45 | { RAN_MSG_HANDOVER_SUCCEEDED, "HANDOVER_SUCCEEDED" }, |
| 46 | { RAN_MSG_HANDOVER_PERFORMED, "HANDOVER_PERFORMED" }, |
| 47 | { RAN_MSG_HANDOVER_REQUIRED, "HANDOVER_REQUIRED" }, |
| 48 | { RAN_MSG_HANDOVER_REQUIRED_REJECT, "HANDOVER_REQUIRED_REJECT" }, |
| 49 | { RAN_MSG_HANDOVER_REQUEST, "HANDOVER_REQUEST" }, |
| 50 | { RAN_MSG_HANDOVER_REQUEST_ACK, "HANDOVER_REQUEST_ACK" }, |
| 51 | { RAN_MSG_HANDOVER_DETECT, "HANDOVER_DETECT" }, |
| 52 | { RAN_MSG_HANDOVER_COMPLETE, "HANDOVER_COMPLETE" }, |
| 53 | { RAN_MSG_HANDOVER_FAILURE, "HANDOVER_FAILURE" }, |
| 54 | {} |
| 55 | }; |
| 56 | |
| 57 | /* extract the N(SD) and return the modulo value for a R99 message */ |
| 58 | static uint8_t ran_dec_dtap_undup_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd) |
| 59 | { |
| 60 | switch (pdisc) { |
| 61 | case GSM48_PDISC_MM: |
| 62 | case GSM48_PDISC_CC: |
| 63 | case GSM48_PDISC_NC_SS: |
| 64 | *n_sd = (msg_type >> 6) & 0x3; |
| 65 | return 4; |
| 66 | case GSM48_PDISC_GROUP_CC: |
| 67 | case GSM48_PDISC_BCAST_CC: |
| 68 | case GSM48_PDISC_LOC: |
| 69 | *n_sd = (msg_type >> 6) & 0x1; |
| 70 | return 2; |
| 71 | default: |
| 72 | /* no sequence number, we cannot detect dups */ |
| 73 | return 0; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /* extract the N(SD) and return the modulo value for a R98 message */ |
| 78 | static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd) |
| 79 | { |
| 80 | switch (pdisc) { |
| 81 | case GSM48_PDISC_MM: |
| 82 | case GSM48_PDISC_CC: |
| 83 | case GSM48_PDISC_NC_SS: |
| 84 | case GSM48_PDISC_GROUP_CC: |
| 85 | case GSM48_PDISC_BCAST_CC: |
| 86 | case GSM48_PDISC_LOC: |
| 87 | *n_sd = (msg_type >> 6) & 0x1; |
| 88 | return 2; |
| 89 | default: |
| 90 | /* no sequence number, we cannot detect dups */ |
| 91 | return 0; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection. |
| 96 | * (Not static for unit testing). */ |
| 97 | int ran_dec_dtap_undup_pdisc_ctr_bin(uint8_t pdisc) |
| 98 | { |
| 99 | switch (pdisc) { |
| 100 | case GSM48_PDISC_MM: |
| 101 | case GSM48_PDISC_CC: |
| 102 | case GSM48_PDISC_NC_SS: |
| 103 | return 0; |
| 104 | case GSM48_PDISC_GROUP_CC: |
| 105 | return 1; |
| 106 | case GSM48_PDISC_BCAST_CC: |
| 107 | return 2; |
| 108 | case GSM48_PDISC_LOC: |
| 109 | return 3; |
| 110 | default: |
| 111 | return -1; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */ |
| 116 | bool ran_dec_dtap_undup_is_duplicate(struct osmo_fsm_inst *log_fi, uint8_t *n_sd_next, bool is_r99, struct msgb *l3) |
| 117 | { |
| 118 | struct gsm48_hdr *gh; |
| 119 | uint8_t pdisc; |
| 120 | uint8_t n_sd, modulo; |
| 121 | int bin; |
| 122 | |
| 123 | gh = msgb_l3(l3); |
| 124 | pdisc = gsm48_hdr_pdisc(gh); |
| 125 | |
| 126 | if (is_r99) { |
| 127 | modulo = ran_dec_dtap_undup_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd); |
| 128 | } else { /* pre R99 */ |
| 129 | modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd); |
| 130 | } |
| 131 | if (modulo == 0) |
| 132 | return false; |
| 133 | bin = ran_dec_dtap_undup_pdisc_ctr_bin(pdisc); |
| 134 | if (bin < 0) |
| 135 | return false; |
| 136 | |
| 137 | OSMO_ASSERT(bin >= 0 && bin < 4); |
| 138 | if (n_sd != n_sd_next[bin]) { |
| 139 | /* not what we expected: duplicate */ |
| 140 | LOGPFSML(log_fi, LOGL_NOTICE, "Duplicate DTAP: bin=%d, expected n_sd == %u, got %u\n", |
| 141 | bin, n_sd_next[bin], n_sd); |
| 142 | return true; |
| 143 | } else { |
| 144 | /* as expected: no dup; update expected counter for next message */ |
| 145 | n_sd_next[bin] = (n_sd + 1) % modulo; |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* convenience: RAN decode implementations can call this to dispatch the decode_cb with a decoded ran_msg. */ |
| 151 | int ran_decoded(struct ran_dec *ran_dec, struct ran_msg *ran_msg) |
| 152 | { |
| 153 | if (!ran_dec->decode_cb) |
| 154 | return -1; |
| 155 | return ran_dec->decode_cb(ran_dec->caller_fi, ran_dec->caller_data, ran_msg); |
| 156 | } |