Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 1 | module BSC_MS_ConnectionHandler { |
| 2 | |
| 3 | import from General_Types all; |
| 4 | import from Osmocom_Types all; |
| 5 | import from SCCPasp_Types all; |
| 6 | import from BSSAP_Types all; |
| 7 | import from BSSMAP_Emulation all; |
| 8 | import from BSSMAP_Templates all; |
| 9 | |
| 10 | import from MobileL3_Types all; |
| 11 | import from MobileL3_CommonIE_Types all; |
| 12 | import from L3_Templates all; |
| 13 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 14 | import from MGCP_Types all; |
| 15 | import from MGCP_Templates all; |
| 16 | import from SDP_Types all; |
| 17 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 18 | /* this component represents a single subscriber connection at the MSC. |
| 19 | * There is a 1:1 mapping between SCCP connections and BSSAP_ConnHdlr components. |
| 20 | * We inherit all component variables, ports, functions, ... from BSSAP_ConnHdlr */ |
| 21 | type component BSC_MS_ConnHdlr extends BSSAP_ConnHdlr { |
| 22 | /* SCCP Connecction Identifier for the underlying SCCP connection */ |
| 23 | var integer g_sccp_conn_id; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 24 | var MgcpConnectionId g_mgcp_conn_id; |
| 25 | var SDP_Message g_sdp; |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 26 | var BSC_State g_state; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* Callback function from general BSSMAP_Emulation whenever a new incoming |
| 30 | * SCCP connection arrivces. Must create + start a new component */ |
Harald Welte | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame] | 31 | private function CreateCallback(ASP_SCCP_N_CONNECT_ind conn_ind, charstring id) |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 32 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 33 | log("Incoming SCCP Connection on BSC ?!?"); |
| 34 | self.stop; |
| 35 | } |
| 36 | |
| 37 | /* Callback function from general BSSMAP_Emulation whenever a connectionless |
| 38 | * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */ |
| 39 | private function UnitdataCallback(PDU_BSSAP bssap) |
| 40 | runs on BSSMAP_Emulation_CT return template PDU_BSSAP { |
| 41 | var template PDU_BSSAP resp := omit; |
| 42 | |
| 43 | if (match(bssap, tr_BSSMAP_Reset)) { |
| 44 | resp := ts_BSSMAP_ResetAck; |
| 45 | } |
| 46 | |
| 47 | return resp; |
| 48 | } |
| 49 | |
| 50 | const BssmapOps BSC_MS_BssmapOps := { |
| 51 | create_cb := refers(CreateCallback), |
| 52 | unitdata_cb := refers(UnitdataCallback) |
| 53 | } |
| 54 | |
| 55 | |
| 56 | function f_gen_cl3(hexstring imsi) return PDU_BSSAP { |
| 57 | var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(imsi)); |
| 58 | var PDU_ML3_MS_NW l3 := valueof(ts_CM_SERV_REQ('0001'B, mi)); |
| 59 | var BSSMAP_IE_CellIdentifier cell_id := valueof(ts_CellID_LAC_CI(23, 42)); |
| 60 | var PDU_BSSAP bssap := valueof(ts_BSSMAP_ComplL3(cell_id, enc_PDU_ML3_MS_NW(l3))); |
| 61 | return bssap; |
| 62 | } |
| 63 | |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 64 | type enumerated BSC_State { |
| 65 | BSC_STATE_NONE, |
| 66 | BSC_STATE_WAIT_ASS_REQ, |
| 67 | BSC_STATE_WAIT_CRCX, |
| 68 | BSC_STATE_WAIT_MDCX, |
| 69 | BSC_STATE_WAIT_CLEAR_CMD, |
| 70 | BSC_STATE_WAIT_DLCX, |
| 71 | BSC_STATE_WAIT_DISC_IND |
| 72 | } |
| 73 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 74 | /* main function processing various incoming events */ |
| 75 | function main(SCCP_PAR_Address sccp_addr_own, SCCP_PAR_Address sccp_addr_remote) |
| 76 | runs on BSC_MS_ConnHdlr { |
| 77 | var PDU_BSSAP bssap; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 78 | var MgcpCommand mgcp_cmd; |
| 79 | var MgcpResponse mgcp_resp; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 80 | |
| 81 | log("Starting main of BSC_MS_ConnHdlr"); |
| 82 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 83 | g_mgcp_conn_id := f_mgcp_alloc_conn_id(); |
| 84 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 85 | /* generate and send the Complete Layer3 Info */ |
| 86 | bssap := f_gen_cl3('901770123456789'H); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 87 | var BSSAP_Conn_Req creq := { |
| 88 | addr_peer := sccp_addr_remote, |
| 89 | addr_own := sccp_addr_own, |
| 90 | bssap := bssap |
| 91 | } |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 92 | g_state := BSC_STATE_WAIT_ASS_REQ; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 93 | BSSAP.send(creq); |
| 94 | |
| 95 | while (true) { |
| 96 | alt { |
| 97 | /* new SCCP-level connection indication from BSC */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 98 | [g_state == BSC_STATE_WAIT_ASS_REQ] BSSAP.receive(tr_BSSMAP_AssignmentReq) -> value bssap { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 99 | /* FIXME: Read CIC */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 100 | /* respond with ASSIGNMENT COMPL */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 101 | g_state := BSC_STATE_WAIT_CRCX; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 102 | BSSAP.send(ts_BSSMAP_AssignmentComplete(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)); |
| 103 | } |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 104 | |
| 105 | /* CRCX -> OK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 106 | [g_state == BSC_STATE_WAIT_CRCX] BSSAP.receive(tr_CRCX) -> value mgcp_cmd { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 107 | /* FIXME: proper SDP parameters */ |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 108 | g_sdp := valueof(ts_SDP("127.0.0.1", "127.0.0.1", "foo", "21", 1000, { "98" }, |
| 109 | { valueof(ts_SDP_rtpmap(98, "AMR/8000")), |
| 110 | valueof(ts_SDP_ptime(20)) })); |
| 111 | /* respond with CRCX_ACK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 112 | g_state := BSC_STATE_WAIT_MDCX; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 113 | BSSAP.send(ts_CRCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id, g_sdp)); |
| 114 | } |
| 115 | |
| 116 | /* MDCX -> OK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 117 | [g_state == BSC_STATE_WAIT_MDCX] BSSAP.receive(tr_MDCX) -> value mgcp_cmd { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 118 | /* FIXME: verify if local part of endpoint name matches CIC */ |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 119 | /* respond with CRCX_ACK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 120 | g_state := BSC_STATE_WAIT_CLEAR_CMD; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 121 | BSSAP.send(ts_MDCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id, g_sdp)); |
| 122 | } |
| 123 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 124 | /* CLEAR COMMAND from MSC; respond with CLEAR COMPLETE) */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 125 | [g_state == BSC_STATE_WAIT_CLEAR_CMD] BSSAP.receive(tr_BSSMAP_ClearCommand) -> value bssap { |
| 126 | g_state := BSC_STATE_WAIT_DLCX; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 127 | BSSAP.send(ts_BSSMAP_ClearComplete); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 130 | /* DLCX -> OK */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 131 | [g_state == BSC_STATE_WAIT_DLCX] BSSAP.receive(tr_DLCX) -> value mgcp_cmd { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 132 | /* FIXME: verify if local part of endpoint name matches CIC */ |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 133 | g_state := BSC_STATE_WAIT_DISC_IND; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 134 | BSSAP.send(ts_DLCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id)); |
| 135 | } |
| 136 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 137 | [] BSSAP.receive(tr_BSSAP_DTAP) -> value bssap { |
Harald Welte | 8f3a962 | 2017-11-25 01:19:13 +0100 | [diff] [blame] | 138 | /* FIXME: verify if local part of endpoint name matches CIC */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 139 | var PDU_ML3_MS_NW l3 := dec_PDU_ML3_MS_NW(bssap.pdu.dtap); |
| 140 | log("Unhandled DTAP ", l3); |
| 141 | } |
| 142 | |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 143 | [g_state == BSC_STATE_WAIT_DISC_IND] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
Harald Welte | 0a4317a | 2017-11-25 00:32:46 +0100 | [diff] [blame] | 144 | setverdict(pass); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 145 | self.stop; |
| 146 | } |
| 147 | |
Harald Welte | a34e47f | 2017-11-25 01:28:54 +0100 | [diff] [blame] | 148 | /* disconnect in invalid state */ |
| 149 | [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
| 150 | setverdict(fail); |
| 151 | self.stop; |
| 152 | } |
| 153 | |
| 154 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 155 | [] BSSAP.receive(PDU_BSSAP:?) -> value bssap { |
| 156 | log("Received unhandled SCCP-CO: ", bssap); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | } |