Harald Welte | d17e75a | 2011-01-18 18:25:20 +0100 | [diff] [blame] | 1 | % SCTP Handler for gateway between MSC and STP, transparently |
| 2 | % rewriting addresses on the fly |
| 3 | |
| 4 | % (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 5 | % (C) 2011 OnWaves |
| 6 | % |
| 7 | % All Rights Reserved |
| 8 | % |
| 9 | % This program is free software; you can redistribute it and/or modify |
| 10 | % it under the terms of the GNU Affero General Public License as |
| 11 | % published by the Free Software Foundation; either version 3 of the |
| 12 | % License, or (at your option) any later version. |
| 13 | % |
| 14 | % This program is distributed in the hope that it will be useful, |
| 15 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | % GNU General Public License for more details. |
| 18 | % |
| 19 | % You should have received a copy of the GNU Affero General Public License |
| 20 | % along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | |
| 23 | -module(sctp_handler). |
| 24 | -author("Harald Welte <laforge@gnumonks.org>"). |
| 25 | -export([init/5]). |
| 26 | |
| 27 | -include_lib("kernel/include/inet.hrl"). |
| 28 | -include_lib("kernel/include/inet_sctp.hrl"). |
| 29 | |
| 30 | -include("m2ua.hrl"). |
| 31 | -include("mtp3.hrl"). |
| 32 | -include("isup.hrl"). |
| 33 | -include("sccp.hrl"). |
| 34 | |
| 35 | -record(loop_data, |
| 36 | {msc_sock, msc_local_ip, msc_remote_ip, msc_remote_port, |
| 37 | msc_local_port, msc_assoc_id, |
| 38 | stp_sock, stp_remote_ip, stp_remote_port, stp_assoc_id |
| 39 | }). |
| 40 | |
| 41 | -define(COMMON_SOCKOPTS, [{active, once}, {reuseaddr, true}]). |
| 42 | |
| 43 | % initialize the sockets towards MSC (listening) and STP (connect) |
| 44 | init(MscLocalIP, MscLocalPort, MscRemoteIP, StpRemoteIP, StpRemotePort) -> |
| 45 | {ok, MscSock} = gen_sctp:open([{ip, MscLocalIP},{port,MscLocalPort}] |
| 46 | ++ ?COMMON_SOCKOPTS), |
| 47 | io:format("Listening for MSC on ~w:~w. ~w~n", |
| 48 | [MscLocalIP, MscLocalPort, MscSock]), |
| 49 | ok = gen_sctp:listen(MscSock, true), |
| 50 | {ok, StpSock} = gen_sctp:open(?COMMON_SOCKOPTS), |
| 51 | L = #loop_data{msc_sock = MscSock, msc_local_ip = MscLocalIP, |
| 52 | msc_remote_ip = MscRemoteIP, |
| 53 | stp_sock = StpSock, stp_remote_ip = StpRemoteIP, |
| 54 | stp_remote_port = StpRemotePort}, |
| 55 | loop(L). |
| 56 | |
| 57 | % initiate a connection to STP as a client |
| 58 | initiate_stp_connection(#loop_data{stp_sock = Sock, stp_remote_ip = IP, stp_remote_port = Port}, Opts) -> |
| 59 | io:format("Establishing SCTP conn to STP ~p Port ~p~n", [IP, Port]), |
| 60 | gen_sctp:connect(Sock, IP, Port, Opts ++ ?COMMON_SOCKOPTS). |
| 61 | |
| 62 | % main loop function |
| 63 | loop(L = #loop_data{msc_sock=MscSock, msc_remote_ip=MscRemoteIp, msc_remote_port=MscRemotePort, |
| 64 | stp_sock=StpSock, stp_remote_ip=StpRemoteIp, stp_remote_port=StpRemotePort}) -> |
| 65 | io:format("Entering receive loop ~p~n", [L]), |
| 66 | io:format("======================================================================~n"), |
| 67 | receive |
| 68 | % MSC connect or disconnect |
| 69 | {sctp, MscSock, MscRemoteIp, Port, {ANC, SAC}} |
| 70 | when is_record(SAC, sctp_assoc_change) -> |
| 71 | io:format("MSC sctp_assoc_change ~p ~p~n", [ANC, SAC]), |
| 72 | #sctp_assoc_change{state = SacState, outbound_streams = OutStreams, |
| 73 | inbound_streams = InStreams, assoc_id = MscAssocId} = SAC, |
| 74 | case SacState of |
| 75 | comm_up -> |
| 76 | InitMsg = #sctp_initmsg{num_ostreams=InStreams, |
| 77 | max_instreams=OutStreams}, |
| 78 | {ok, StpAssoc} = initiate_stp_connection(L, [{sctp_initmsg,InitMsg}]), |
| 79 | io:format("STP Assoc: ~p~n", [StpAssoc]), |
| 80 | NewL = L#loop_data{msc_remote_port = Port, |
| 81 | msc_assoc_id = MscAssocId, |
| 82 | stp_assoc_id = StpAssoc#sctp_assoc_change.assoc_id}; |
| 83 | comm_lost -> |
| 84 | NewL = L, |
| 85 | % maybe we should simply die? |
| 86 | foo:bar(); |
| 87 | addr_unreachable -> |
| 88 | NewL = L, |
| 89 | % maybe we should simply die? |
| 90 | foo:bar() |
| 91 | end, |
| 92 | inet:setopts(MscSock, [{active, once}]); |
| 93 | % STP connect or disconnect |
| 94 | {sctp, StpSock, StpRemoteIp, StpRemotePort, {_Anc, SAC}} |
| 95 | when is_record(SAC, sctp_assoc_change) -> |
| 96 | io:format("STP sctp_assoc_change ~p~n", [SAC]), |
| 97 | inet:setopts(StpSock, [{active, once}]), |
| 98 | NewL = L; |
| 99 | % MSC data |
| 100 | {sctp, MscSock, MscRemoteIp, MscRemotePort, {[Anc], Data}} -> |
| 101 | io:format("MSC rx data: ~p ~p~n", [Anc, Data]), |
| 102 | handle_rx_data(L, from_msc, Anc, Data), |
| 103 | inet:setopts(MscSock, [{active, once}]), |
| 104 | NewL = L; |
| 105 | % STP data |
| 106 | {sctp, StpSock, StpRemoteIp, StpRemotePort, {[Anc], Data}} -> |
| 107 | io:format("STP rx data: ~p ~p~n", [Anc, Data]), |
| 108 | handle_rx_data(L, from_stp, Anc, Data), |
| 109 | inet:setopts(StpSock, [{active, once}]), |
| 110 | NewL = L; |
| 111 | Other -> |
| 112 | io:format("OTHER ~p~n", [Other]), |
| 113 | NewL = L |
| 114 | end, |
| 115 | loop(NewL). |
| 116 | |
| 117 | |
| 118 | % handle incoming data on one of the SCTP sockets |
| 119 | handle_rx_data(L, From, SRInf = #sctp_sndrcvinfo{ppid = 2, |
| 120 | stream = Stream}, Data) when is_binary(Data) -> |
| 121 | DataOut = mangle_rx_data(L, From, Data), |
| 122 | % send mangled data to other peer |
| 123 | case From of |
| 124 | from_msc -> |
| 125 | Sock = L#loop_data.stp_sock, |
| 126 | AssocId = L#loop_data.stp_assoc_id; |
| 127 | from_stp -> |
| 128 | Sock = L#loop_data.msc_sock, |
| 129 | AssocId = L#loop_data.msc_assoc_id |
| 130 | end, |
| 131 | SndRcvInfo = #sctp_sndrcvinfo{ppid = 2, stream = Stream, assoc_id = AssocId}, |
| 132 | io:format("Sending ~p to ~p ~p~n", [DataOut, Sock, SndRcvInfo]), |
| 133 | % if they are not equal, we will abort here |
| 134 | DataOut = Data, |
| 135 | io:format("Data is equal~n"), |
| 136 | ok = gen_sctp:send(Sock, SndRcvInfo, DataOut). |
| 137 | |
| 138 | % mangle the received data |
| 139 | mangle_rx_data(L, From, Data) when is_binary(Data) -> |
| 140 | {ok, M2ua} = m2ua_codec:parse_m2ua_msg(Data), |
| 141 | io:format("M2UA Decode: ~p~n", [M2ua]), |
| 142 | case M2ua of |
| 143 | #m2ua_msg{msg_class = ?M2UA_MSGC_MAUP, |
| 144 | msg_type = ?M2UA_MAUP_MSGT_DATA} -> |
| 145 | M2ua_out = mangle_rx_m2ua_maup(L, From, M2ua); |
| 146 | #m2ua_msg{} -> |
| 147 | % simply pass it along unmodified |
| 148 | M2ua_out = M2ua |
| 149 | end, |
| 150 | % re-encode the data |
| 151 | io:format("M2UA Encode: ~p~n", [M2ua_out]), |
| 152 | m2ua_codec:encode_m2ua_msg(M2ua_out). |
| 153 | |
| 154 | % mangle the received M2UA |
| 155 | mangle_rx_m2ua_maup(L, From, M2ua = #m2ua_msg{parameters = Params}) -> |
| 156 | {_Len, M2uaPayload} = proplists:get_value(16#300, Params), |
| 157 | Mtp3 = mtp3_codec:parse_mtp3_msg(M2uaPayload), |
| 158 | io:format("MTP3 Decode: ~p~n", [Mtp3]), |
| 159 | Mtp3_out = mangle_rx_mtp3(L, From, Mtp3), |
| 160 | io:format("MTP3 Encode: ~p~n", [Mtp3_out]), |
| 161 | Mtp3OutBin = mtp3_codec:encode_mtp3_msg(Mtp3_out), |
| 162 | Params2 = proplists:delete(16#300, Params), |
| 163 | ParamsNew = Params2 ++ [{16#300, {byte_size(Mtp3OutBin), Mtp3OutBin}}], |
| 164 | % return mangled parsed m2ua msg |
| 165 | M2ua#m2ua_msg{parameters = ParamsNew}. |
| 166 | |
| 167 | % mangle the MTP3 payload |
| 168 | mangle_rx_mtp3(L, From, Mtp3 = #mtp3_msg{service_ind = Service}) -> |
| 169 | mangle_rx_mtp3_serv(L, From, Service, Mtp3). |
| 170 | |
| 171 | % mangle the ISUP content |
| 172 | mangle_rx_mtp3_serv(L, From, ?MTP3_SERV_ISUP, Mtp3 = #mtp3_msg{payload = Payload}) -> |
| 173 | io:format("ISUP~n"), |
| 174 | % FIXME |
| 175 | %Isup = parse_isup_msg(Payload), |
| 176 | Mtp3; |
| 177 | % mangle the SCCP content |
| 178 | mangle_rx_mtp3_serv(L, From, ?MTP3_SERV_SCCP, Mtp3 = #mtp3_msg{payload = Payload}) -> |
| 179 | Sccp = sccp_codec:parse_sccp_msg(Payload), |
| 180 | io:format("SCCP Decode: ~p~n", [Sccp]), |
| 181 | % FIXME |
| 182 | Mtp3; |
| 183 | % default: do nothing |
| 184 | mangle_rx_mtp3_serv(_L, _From, _, Mtp3) -> |
| 185 | Mtp3. |