Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 1 | module BSC_ConnectionHandler { |
| 2 | |
| 3 | import from General_Types all; |
| 4 | import from Osmocom_Types all; |
| 5 | import from GSM_Types all; |
| 6 | import from SCCPasp_Types all; |
| 7 | import from BSSAP_Types all; |
| 8 | import from BSSMAP_Emulation all; |
| 9 | import from BSSMAP_Templates all; |
| 10 | |
| 11 | import from GSUP_Types all; |
| 12 | import from GSUP_Emulation all; |
| 13 | |
| 14 | import from MNCC_Types all; |
| 15 | import from MNCC_Emulation all; |
| 16 | |
Harald Welte | 4aa970c | 2018-01-26 10:38:09 +0100 | [diff] [blame] | 17 | import from MGCP_Types all; |
| 18 | import from MGCP_Emulation all; |
| 19 | |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 20 | import from MobileL3_Types all; |
| 21 | import from MobileL3_CommonIE_Types all; |
| 22 | import from MobileL3_MM_Types all; |
| 23 | import from L3_Templates all; |
| 24 | |
| 25 | /* this component represents a single subscriber connection */ |
Harald Welte | 4aa970c | 2018-01-26 10:38:09 +0100 | [diff] [blame] | 26 | type component BSC_ConnHdlr extends BSSAP_ConnHdlr, MNCC_ConnHdlr, GSUP_ConnHdlr, MGCP_ConnHdlr { |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 27 | var BSC_ConnHdlrPars g_pars; |
| 28 | } |
| 29 | |
Harald Welte | 148a708 | 2018-01-26 18:56:43 +0100 | [diff] [blame^] | 30 | type record AuthVector { |
| 31 | OCT16 rand, |
| 32 | OCT4 sres, |
| 33 | OCT8 kc |
| 34 | /* FIXME: 3G elements */ |
| 35 | } |
| 36 | |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 37 | type record BSC_ConnHdlrPars { |
| 38 | SCCP_PAR_Address sccp_addr_own, |
| 39 | SCCP_PAR_Address sccp_addr_peer, |
| 40 | BSSMAP_IE_CellIdentifier cell_id, |
Harald Welte | 256571e | 2018-01-24 18:47:19 +0100 | [diff] [blame] | 41 | hexstring imei, |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 42 | hexstring imsi, |
Harald Welte | 8260057 | 2018-01-21 20:54:08 +0100 | [diff] [blame] | 43 | hexstring msisdn, |
Harald Welte | 256571e | 2018-01-24 18:47:19 +0100 | [diff] [blame] | 44 | OCT4 tmsi optional, |
Harald Welte | 8260057 | 2018-01-21 20:54:08 +0100 | [diff] [blame] | 45 | BSSMAP_IE_ClassmarkInformationType2 cm2, |
Harald Welte | 1611428 | 2018-01-24 22:41:21 +0100 | [diff] [blame] | 46 | BSSMAP_IE_ClassmarkInformationType3 cm3 optional, |
Harald Welte | 148a708 | 2018-01-26 18:56:43 +0100 | [diff] [blame^] | 47 | AuthVector vec optional |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | |
| 51 | /* Callback function from general BSSMAP_Emulation whenever a connectionless |
| 52 | * BSSMAP message arrives. Canreturn a PDU_BSSAPthat should be sent in return */ |
| 53 | private function BscUnitdataCallback(PDU_BSSAP bssap) |
| 54 | runs on BSSMAP_Emulation_CT return template PDU_BSSAP { |
| 55 | var template PDU_BSSAP resp := omit; |
| 56 | |
| 57 | log("BSSMAP_BscUnitdataCallback"); |
| 58 | /* answer all RESET with RESET ACK */ |
| 59 | if (match(bssap, tr_BSSMAP_Reset)){ |
| 60 | log("BSSMAP_BscUnitdataCallback: Responding to RESET with RESET-ACK"); |
| 61 | resp := ts_BSSMAP_ResetAck; |
| 62 | } |
| 63 | |
| 64 | /* FIXME: Handle paging, etc. */ |
| 65 | return resp; |
| 66 | } |
| 67 | |
| 68 | const BssmapOps BSC_BssmapOps := { |
| 69 | /* Create call-back for inbound connections from MSC (hand-over) */ |
| 70 | create_cb := refers(BSSMAP_Emulation.ExpectedCreateCallback), |
| 71 | unitdata_cb := refers(BscUnitdataCallback), |
| 72 | decode_dtap := true, |
| 73 | role_ms := true |
| 74 | } |
| 75 | |
| 76 | |
| 77 | private function MnccUnitdataCallback(MNCC_PDU mncc) |
| 78 | runs on MNCC_Emulation_CT return template MNCC_PDU { |
| 79 | log("Ignoring MNCC", mncc); |
| 80 | return omit; |
| 81 | } |
| 82 | |
| 83 | const MnccOps BCC_MnccOps := { |
| 84 | create_cb := refers(MNCC_Emulation.ExpectedCreateCallback), |
| 85 | unitdata_cb := refers(MnccUnitdataCallback) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | template BSSAP_Conn_Req ts_BSSAP_Conn_Req(SCCP_PAR_Address peer, SCCP_PAR_Address own, PDU_BSSAP bssap) := { |
| 91 | addr_peer := peer, |
| 92 | addr_own := own, |
| 93 | bssap := bssap |
| 94 | }; |
| 95 | |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 96 | /* Encode 'l3' and ask BSSMAP_Emulation to create new connection with COMPL L3 INFO */ |
| 97 | function f_bssap_compl_l3(PDU_ML3_MS_NW l3) |
| 98 | runs on BSC_ConnHdlr { |
| 99 | log("Sending COMPL L3: ", l3); |
| 100 | var octetstring l3_enc := enc_PDU_ML3_MS_NW(l3); |
| 101 | BSSAP.send(ts_BSSAP_Conn_Req(g_pars.sccp_addr_peer, g_pars.sccp_addr_own, |
| 102 | valueof(ts_BSSMAP_ComplL3(g_pars.cell_id, l3_enc)))); |
Harald Welte | 71b6933 | 2018-01-21 20:43:53 +0100 | [diff] [blame] | 103 | alt { |
| 104 | [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_CONF_IND) {} |
| 105 | [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
| 106 | setverdict(fail, "DISC.ind from SCCP"); |
| 107 | self.stop; |
| 108 | } |
| 109 | } |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* helper function to fully establish a dedicated channel */ |
Harald Welte | 148a708 | 2018-01-26 18:56:43 +0100 | [diff] [blame^] | 113 | function f_establish_fully(MobileIdentityLV mi, boolean expect_auth, boolean expect_ciph) |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 114 | runs on BSC_ConnHdlr { |
Harald Welte | 6ed6bf9 | 2018-01-24 21:09:15 +0100 | [diff] [blame] | 115 | var PDU_ML3_MS_NW l3_info := valueof(ts_CM_SERV_REQ(CM_TYPE_MO_CALL, mi)); |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 116 | var PDU_DTAP_MT dtap_mt; |
| 117 | |
| 118 | /* Send BSSAP_Conn_Req with COMPL L3 INFO to MSC */ |
| 119 | f_bssap_compl_l3(l3_info); |
| 120 | |
Harald Welte | 148a708 | 2018-01-26 18:56:43 +0100 | [diff] [blame^] | 121 | f_mm_common(expect_auth, expect_ciph); |
| 122 | if (expect_ciph) { |
| 123 | /* implicit CM SERVICE ACCEPT? */ |
| 124 | } else { |
| 125 | /* explicit CM SERVICE ACCEPT */ |
| 126 | BSSAP.receive(tr_PDU_DTAP_MT(tr_CM_SERV_ACC)); |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 127 | } |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* build a PDU_ML3_MS_NW containing a Location Update by IMSI */ |
| 131 | function f_build_lu_imsi(hexstring imsi) return PDU_ML3_MS_NW |
| 132 | { |
| 133 | var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(imsi)); |
| 134 | return f_build_lu(mi); |
| 135 | } |
Harald Welte | ba7b6d9 | 2018-01-23 21:32:34 +0100 | [diff] [blame] | 136 | function f_build_lu_imei(hexstring imei) return PDU_ML3_MS_NW |
| 137 | { |
| 138 | var MobileIdentityLV mi := valueof(ts_MI_IMEI_LV(imei)); |
| 139 | return f_build_lu(mi); |
| 140 | } |
| 141 | function f_build_lu_tmsi(OCT4 tmsi) return PDU_ML3_MS_NW |
| 142 | { |
| 143 | var MobileIdentityLV mi := valueof(ts_MI_TMSI_LV(tmsi)); |
| 144 | return f_build_lu(mi); |
| 145 | } |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 146 | private function f_build_lu(MobileIdentityLV mi) return PDU_ML3_MS_NW |
| 147 | { |
| 148 | var LocationAreaIdentification_V old_lai := { '62F220'O, '9999'O }; |
| 149 | var PDU_ML3_MS_NW l3_info := valueof(ts_ML3_MO_LU_Req(valueof(ts_ML3_IE_LuType_Attach), |
| 150 | old_lai, mi, valueof(ts_CM1))); |
| 151 | return l3_info; |
| 152 | } |
| 153 | |
Harald Welte | cf66d5a | 2018-01-23 19:24:28 +0100 | [diff] [blame] | 154 | private function f_rnd_oct(integer len) return octetstring { |
| 155 | var integer i; |
| 156 | var octetstring res; |
| 157 | for (i := 0; i < len; i := i + 1) { |
| 158 | res[i] := int2oct(float2int(rnd()*256.0), 1); |
| 159 | } |
| 160 | return res; |
| 161 | } |
| 162 | |
| 163 | function f_gen_auth_vec_2g() return AuthVector { |
| 164 | var AuthVector vec; |
| 165 | vec.rand := f_rnd_oct(16); |
| 166 | vec.sres := f_rnd_oct(4); |
| 167 | vec.kc := f_rnd_oct(8); |
| 168 | return vec; |
| 169 | } |
| 170 | |
Harald Welte | 148a708 | 2018-01-26 18:56:43 +0100 | [diff] [blame^] | 171 | |
| 172 | function f_mm_common(boolean expect_auth, boolean expect_ciph) runs on BSC_ConnHdlr |
| 173 | { |
| 174 | if (expect_auth) { |
| 175 | g_pars.vec := f_gen_auth_vec_2g(); |
| 176 | var GSUP_IE auth_tuple := valueof(ts_GSUP_IE_AuthTuple2G(g_pars.vec.rand, |
| 177 | g_pars.vec.sres, |
| 178 | g_pars.vec.kc)); |
| 179 | GSUP.receive(tr_GSUP_SAI_REQ(g_pars.imsi)); |
| 180 | GSUP.send(ts_GSUP_SAI_RES(g_pars.imsi, auth_tuple)); |
| 181 | |
| 182 | BSSAP.receive(tr_PDU_DTAP_MT(tr_ML3_MT_MM_AUTH_REQ(g_pars.vec.rand))); |
| 183 | BSSAP.send(ts_PDU_DTAP_MO(ts_ML3_MT_MM_AUTH_RESP_2G(g_pars.vec.sres))); |
| 184 | } |
| 185 | |
| 186 | if (expect_ciph) { |
| 187 | BSSAP.receive(tr_BSSMAP_CipherModeCmd(?, g_pars.vec.kc)); |
| 188 | BSSAP.send(ts_BSSMAP_CipherModeCompl('02'O)); |
| 189 | } |
| 190 | } |
| 191 | |
Harald Welte | 1611428 | 2018-01-24 22:41:21 +0100 | [diff] [blame] | 192 | function f_perform_lu(boolean expect_auth, boolean expect_tmsi, boolean send_early_cm, |
| 193 | boolean expect_ciph := false) |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 194 | runs on BSC_ConnHdlr { |
| 195 | var PDU_ML3_MS_NW l3_lu := f_build_lu_imsi(g_pars.imsi) |
| 196 | var PDU_DTAP_MT dtap_mt; |
| 197 | |
| 198 | /* tell GSUP dispatcher to send this IMSI to us */ |
| 199 | f_create_gsup_expect(hex2str(g_pars.imsi)); |
| 200 | |
| 201 | /* Send BSSAP_Conn_Req with COMPL L3 INFO to MSC */ |
| 202 | f_bssap_compl_l3(l3_lu); |
| 203 | |
Harald Welte | 8a121b3 | 2018-01-22 03:00:41 +0100 | [diff] [blame] | 204 | if (send_early_cm) { |
| 205 | BSSAP.send(ts_BSSMAP_ClassmarkUpd(g_pars.cm2, g_pars.cm3)); |
| 206 | } |
Harald Welte | 5c2622c | 2018-01-21 20:45:20 +0100 | [diff] [blame] | 207 | |
Harald Welte | 148a708 | 2018-01-26 18:56:43 +0100 | [diff] [blame^] | 208 | f_mm_common(expect_auth, expect_ciph); |
Harald Welte | 1611428 | 2018-01-24 22:41:21 +0100 | [diff] [blame] | 209 | |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 210 | /* Expect MSC to perform LU with HLR */ |
| 211 | GSUP.receive(tr_GSUP_UL_REQ(g_pars.imsi)); |
| 212 | GSUP.send(ts_GSUP_ISD_REQ(g_pars.imsi, g_pars.msisdn)); |
| 213 | GSUP.receive(tr_GSUP_ISD_RES(g_pars.imsi)); |
| 214 | GSUP.send(ts_GSUP_UL_RES(g_pars.imsi)); |
| 215 | |
| 216 | alt { |
| 217 | [] BSSAP.receive(tr_PDU_DTAP_MT(tr_ML3_MT_LU_Acc)) -> value dtap_mt { |
| 218 | var PDU_ML3_LocationUpdateAccept lu_acc := dtap_mt.dtap.msgs.mm.locationUpdateAccept; |
| 219 | if (expect_tmsi) { |
| 220 | if (not ispresent(lu_acc.mobileIdentityTLV) or |
| 221 | not ischosen(lu_acc.mobileIdentityTLV.mobileIdentityLV.mobileIdentityV.oddEvenInd_identity.tmsi_ptmsi)) { |
| 222 | setverdict(fail, "Expected TMSI but no TMSI was allocated"); |
| 223 | self.stop; |
| 224 | } else { |
Harald Welte | 256571e | 2018-01-24 18:47:19 +0100 | [diff] [blame] | 225 | g_pars.tmsi := lu_acc.mobileIdentityTLV.mobileIdentityLV.mobileIdentityV.oddEvenInd_identity.tmsi_ptmsi.octets; |
Harald Welte | a49e36e | 2018-01-21 19:29:33 +0100 | [diff] [blame] | 226 | BSSAP.send(ts_PDU_DTAP_MO(ts_ML3_MO_TmsiRealloc_Cmpl)); |
| 227 | } |
| 228 | } else { |
| 229 | if (ispresent(lu_acc.mobileIdentityTLV) and |
| 230 | ischosen(lu_acc.mobileIdentityTLV.mobileIdentityLV.mobileIdentityV.oddEvenInd_identity.tmsi_ptmsi)) { |
| 231 | setverdict(fail, "Expected no TMSI but TMSI was allocated"); |
| 232 | self.stop; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | [] BSSAP.receive(tr_PDU_DTAP_MT(tr_ML3_MT_LU_Rej)) { |
| 237 | setverdict(fail, "Expected LU ACK, but received LU REJ"); |
| 238 | self.stop; |
| 239 | } |
| 240 | } |
| 241 | /* FIXME: there could be pending SMS or other common procedures by the MSC, let's ignore them */ |
| 242 | BSSAP.receive(tr_BSSMAP_ClearCommand); |
| 243 | BSSAP.send(ts_BSSMAP_ClearComplete); |
| 244 | BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND); |
| 245 | setverdict(pass); |
| 246 | } |
| 247 | |
| 248 | function f_foo() runs on BSC_ConnHdlr{ |
| 249 | /* SCCP CC handled by BSSMAP_Emulation_CT.main() */ |
| 250 | /* Expect auth, if enabled */ |
| 251 | |
| 252 | /* TODO: ISD */ |
| 253 | /* Expect encr, if enabled */ |
| 254 | /* Expect encr, if enabled */ |
| 255 | /* Expect ASS CMD, if chan_type != requested */ |
| 256 | /* Send ASS CMPL in successful case */ |
| 257 | |
| 258 | /* Expect AoIP port/ip information for RTP stream */ |
| 259 | /* Expect MSC-originated MGCP to our simulated MGW */ |
| 260 | /* Verify Counters via CTRL */ |
| 261 | /* re-configure MSC behaviour via VTY */ |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | } |
| 269 | |
| 270 | |