Harald Welte | aca4edc | 2011-01-21 16:21:12 +0000 | [diff] [blame] | 1 | % |
| 2 | |
| 3 | % (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | % (C) 2011 OnWaves |
| 5 | % |
| 6 | % All Rights Reserved |
| 7 | % |
| 8 | % This program is free software; you can redistribute it and/or modify |
| 9 | % it under the terms of the GNU Affero General Public License as |
| 10 | % published by the Free Software Foundation; either version 3 of the |
| 11 | % License, or (at your option) any later version. |
| 12 | % |
| 13 | % This program is distributed in the hope that it will be useful, |
| 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | % GNU General Public License for more details. |
| 17 | % |
| 18 | % You should have received a copy of the GNU Affero General Public License |
| 19 | % along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | -module(mgw_nat). |
| 22 | -author("Harald Welte <laforge@gnumonks.org>"). |
| 23 | -export([mangle_rx_data/3]). |
| 24 | |
| 25 | %-include_lib("kernel/include/inet.hrl"). |
| 26 | %-include_lib("kernel/include/inet_sctp.hrl"). |
| 27 | |
| 28 | -include("m2ua.hrl"). |
| 29 | -include("mtp3.hrl"). |
| 30 | -include("isup.hrl"). |
| 31 | -include("sccp.hrl"). |
| 32 | |
| 33 | % mangle the received data |
| 34 | mangle_rx_data(L, From, Data) when is_binary(Data) -> |
| 35 | {ok, M2ua} = m2ua_codec:parse_m2ua_msg(Data), |
| 36 | %io:format("M2UA Decode: ~p~n", [M2ua]), |
| 37 | case M2ua of |
| 38 | #m2ua_msg{msg_class = ?M2UA_MSGC_MAUP, |
| 39 | msg_type = ?M2UA_MAUP_MSGT_DATA} -> |
| 40 | M2ua_out = mangle_rx_m2ua_maup(L, From, M2ua); |
| 41 | #m2ua_msg{} -> |
| 42 | % simply pass it along unmodified |
| 43 | M2ua_out = M2ua |
| 44 | end, |
| 45 | % re-encode the data |
| 46 | %io:format("M2UA Encode: ~p~n", [M2ua_out]), |
| 47 | m2ua_codec:encode_m2ua_msg(M2ua_out). |
| 48 | |
| 49 | % mangle the received M2UA |
| 50 | mangle_rx_m2ua_maup(L, From, M2ua = #m2ua_msg{parameters = Params}) -> |
| 51 | {_Len, M2uaPayload} = proplists:get_value(16#300, Params), |
| 52 | Mtp3 = mtp3_codec:parse_mtp3_msg(M2uaPayload), |
| 53 | %io:format("MTP3 Decode: ~p~n", [Mtp3]), |
| 54 | Mtp3_out = mangle_rx_mtp3(L, From, Mtp3), |
| 55 | %io:format("MTP3 Encode: ~p~n", [Mtp3_out]), |
| 56 | Mtp3OutBin = mtp3_codec:encode_mtp3_msg(Mtp3_out), |
| 57 | Params2 = proplists:delete(16#300, Params), |
| 58 | ParamsNew = Params2 ++ [{16#300, {byte_size(Mtp3OutBin), Mtp3OutBin}}], |
| 59 | % return mangled parsed m2ua msg |
| 60 | M2ua#m2ua_msg{parameters = ParamsNew}. |
| 61 | |
| 62 | % mangle the MTP3 payload |
| 63 | mangle_rx_mtp3(L, From, Mtp3 = #mtp3_msg{service_ind = Service}) -> |
| 64 | mangle_rx_mtp3_serv(L, From, Service, Mtp3). |
| 65 | |
| 66 | % mangle the ISUP content |
| 67 | mangle_rx_mtp3_serv(L, From, ?MTP3_SERV_ISUP, Mtp3 = #mtp3_msg{payload = Payload}) -> |
| 68 | io:format("ISUP In: ~p~n", [Payload]), |
| 69 | Isup = isup_codec:parse_isup_msg(Payload), |
| 70 | io:format("ISUP Decode: ~p~n", [Isup]), |
| 71 | % FIXME |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 72 | IsupMangled = mangle_rx_isup(From, Isup#isup_msg.msg_type, Isup), |
| 73 | if IsupMangled == Isup -> |
| 74 | Mtp3; |
| 75 | true -> |
| 76 | io:format("ISUP Encode In: ~p~n", [IsupMangled]), |
| 77 | Payload_out = isup_codec:encode_isup_msg(IsupMangled), |
| 78 | io:format("ISUP Encode Out: ~p~n", [Payload_out]), |
| 79 | % return modified MTP3 payload |
| 80 | Mtp3#mtp3_msg{payload = Payload_out} |
Harald Welte | aca4edc | 2011-01-21 16:21:12 +0000 | [diff] [blame] | 81 | end; |
| 82 | % mangle the SCCP content |
| 83 | mangle_rx_mtp3_serv(L, From, ?MTP3_SERV_SCCP, Mtp3 = #mtp3_msg{payload = Payload}) -> |
| 84 | Sccp = sccp_codec:parse_sccp_msg(Payload), |
| 85 | io:format("SCCP Decode: ~p~n", [Sccp]), |
| 86 | % FIXME |
| 87 | Mtp3; |
| 88 | % default: do nothing |
| 89 | mangle_rx_mtp3_serv(_L, _From, _, Mtp3) -> |
| 90 | Mtp3. |
| 91 | |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 92 | |
| 93 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 94 | % Actual mangling of the decoded ISUP messages |
| 95 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 96 | |
Harald Welte | f62f0ed | 2011-01-22 10:13:34 +0100 | [diff] [blame] | 97 | -define(MSRN_PFX_MSC, [3,5,4,8,9,0,9,9]). |
| 98 | -define(MSRN_PFX_STP, [6,3,9,2,9,9,4,2,0,0]). |
Harald Welte | 7871c75 | 2011-01-21 19:25:23 +0000 | [diff] [blame] | 99 | -define(INTERN_PFX, [6,3]). |
Harald Welte | aca4edc | 2011-01-21 16:21:12 +0000 | [diff] [blame] | 100 | |
Harald Welte | 2fc3e98 | 2011-01-21 18:50:36 +0000 | [diff] [blame] | 101 | % iterate over list of parameters and call mangle_rx_isup_par() for each one |
| 102 | mangle_rx_isup_params(_From, _MsgType, _Msg, ParListOut, []) -> |
| 103 | ParListOut; |
| 104 | mangle_rx_isup_params(From, MsgType, Msg, ParListOut, [Par|ParList]) -> |
| 105 | ParOut = mangle_rx_isup_par(From, MsgType, Msg, Par), |
| 106 | mangle_rx_isup_params(From, MsgType, Msg, ParListOut++[ParOut], ParList). |
| 107 | |
| 108 | % manipulate phone numbers |
| 109 | mangle_rx_isup_par(From, MsgType, Msg, {ParType, ParBody}) when |
| 110 | ParType == ?ISUP_PAR_CALLED_P_NUM; |
| 111 | ParType == ?ISUP_PAR_CALLING_P_NUM -> |
| 112 | NewParBody = mangle_isup_number(From, MsgType, ParType, ParBody), |
| 113 | {ParType, NewParBody}; |
| 114 | % defauly case: do not mangle this parameter |
| 115 | mangle_rx_isup_par(_From, _MsgType, _Msg, Par) -> |
| 116 | Par. |
| 117 | |
| 118 | % mangle an incoming ISUP message |
| 119 | mangle_rx_isup(From, MsgType, Msg = #isup_msg{parameters = Params}) -> |
| 120 | ParamsOut = mangle_rx_isup_params(From, MsgType, Msg, [], Params), |
| 121 | % return message with modified parameter list |
| 122 | Msg#isup_msg{parameters = ParamsOut}. |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 123 | |
Harald Welte | 62469be | 2011-01-21 22:47:42 +0100 | [diff] [blame] | 124 | % STP->MSC: Mangle a Party Number in IAM |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 125 | mangle_isup_number(from_stp, ?ISUP_MSGT_IAM, NumType, PartyNum) -> |
| 126 | case NumType of |
| 127 | ?ISUP_PAR_CALLED_P_NUM -> |
Harald Welte | f62f0ed | 2011-01-22 10:13:34 +0100 | [diff] [blame] | 128 | % First convert to international number, if it is national |
| 129 | Num1 = isup_party_internationalize(PartyNum, ?INTERN_PFX), |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 130 | io:format("IAM MSRN rewrite (STP->MSC): "), |
Harald Welte | f62f0ed | 2011-01-22 10:13:34 +0100 | [diff] [blame] | 131 | isup_party_replace_prefix(Num1, ?MSRN_PFX_STP, ?MSRN_PFX_MSC); |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 132 | _ -> |
| 133 | PartyNum |
Harald Welte | acb8c2c | 2011-01-21 17:26:53 +0000 | [diff] [blame] | 134 | end; |
Harald Welte | 62469be | 2011-01-21 22:47:42 +0100 | [diff] [blame] | 135 | % MSC->STP: Mangle connected number in response to IAM |
Harald Welte | 9344354 | 2011-01-21 18:56:58 +0000 | [diff] [blame] | 136 | mangle_isup_number(from_msc, MsgT, NumType, PartyNum) when MsgT == ?ISUP_MSGT_CON; |
| 137 | MsgT == ?ISUP_MSGT_ANM -> |
| 138 | case NumType of |
| 139 | ?ISUP_PAR_CONNECTED_NUM -> |
| 140 | io:format("CON MSRN rewrite (MSC->STP): "), |
Harald Welte | 62469be | 2011-01-21 22:47:42 +0100 | [diff] [blame] | 141 | Num1 = isup_party_replace_prefix(PartyNum, ?MSRN_PFX_MSC, ?MSRN_PFX_STP), |
Harald Welte | f62f0ed | 2011-01-22 10:13:34 +0100 | [diff] [blame] | 142 | % Second: convert to national number, if it is international |
| 143 | isup_party_nationalize(Num1, ?INTERN_PFX); |
Harald Welte | 9344354 | 2011-01-21 18:56:58 +0000 | [diff] [blame] | 144 | _ -> |
| 145 | PartyNum |
| 146 | end; |
Harald Welte | 62469be | 2011-01-21 22:47:42 +0100 | [diff] [blame] | 147 | % MAC->STP: Mangle IAM international -> national |
Harald Welte | d25a454 | 2011-01-21 22:42:45 +0100 | [diff] [blame] | 148 | mangle_isup_number(from_msc, ?ISUP_MSGT_IAM, NumType, PartyNum) -> |
| 149 | case NumType of |
| 150 | ?ISUP_PAR_CALLED_P_NUM -> |
| 151 | isup_party_nationalize(PartyNum, ?INTERN_PFX); |
| 152 | _ -> |
| 153 | PartyNum |
| 154 | end; |
Harald Welte | 62469be | 2011-01-21 22:47:42 +0100 | [diff] [blame] | 155 | % STP->MSC: Mangle connected number in response to IAM (national->international) |
| 156 | mangle_isup_number(from_stp, MsgT, NumType, PartyNum) when MsgT == ?ISUP_MSGT_CON; |
| 157 | MsgT == ?ISUP_MSGT_ANM -> |
| 158 | case NumType of |
| 159 | ?ISUP_PAR_CONNECTED_NUM -> |
| 160 | isup_party_internationalize(PartyNum, ?INTERN_PFX); |
| 161 | _ -> |
| 162 | PartyNum |
| 163 | end; |
Harald Welte | acb8c2c | 2011-01-21 17:26:53 +0000 | [diff] [blame] | 164 | % default case: no rewrite |
| 165 | mangle_isup_number(from_msc, _, _, PartyNum) -> |
| 166 | PartyNum. |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 167 | |
| 168 | % replace the prefix of PartyNum with NewPfx _if_ the current prefix matches MatchPfx |
Harald Welte | 7871c75 | 2011-01-21 19:25:23 +0000 | [diff] [blame] | 169 | isup_party_replace_prefix(PartyNum, MatchPfx, NewPfx) -> |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 170 | DigitsIn = PartyNum#party_number.phone_number, |
| 171 | MatchPfxLen = length(MatchPfx), |
| 172 | Pfx = lists:sublist(DigitsIn, 1, MatchPfxLen), |
| 173 | if Pfx == MatchPfx -> |
Harald Welte | 1df3c55 | 2011-01-21 17:33:45 +0000 | [diff] [blame] | 174 | Trailer = lists:sublist(DigitsIn, MatchPfxLen+1, length(DigitsIn)-MatchPfxLen), |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 175 | DigitsOut = NewPfx ++ Trailer, |
Harald Welte | 7871c75 | 2011-01-21 19:25:23 +0000 | [diff] [blame] | 176 | io:format("Prefix rewrite: ~p -> ~p~n", [DigitsIn, DigitsOut]); |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 177 | true -> |
Harald Welte | 7871c75 | 2011-01-21 19:25:23 +0000 | [diff] [blame] | 178 | io:format("Prefix rewrite: NO MATCH (~p != ~p)~n", [Pfx, MatchPfx]), |
Harald Welte | 49ec10e | 2011-01-21 17:21:13 +0000 | [diff] [blame] | 179 | DigitsOut = DigitsIn |
| 180 | end, |
| 181 | PartyNum#party_number{phone_number = DigitsOut}. |
Harald Welte | 7871c75 | 2011-01-21 19:25:23 +0000 | [diff] [blame] | 182 | |
| 183 | isup_party_internationalize(PartyNum, CountryCode) -> |
| 184 | #party_number{phone_number = DigitsIn, nature_of_addr_ind = Nature} = PartyNum, |
| 185 | case Nature of |
| 186 | ?ISUP_ADDR_NAT_NATIONAL -> |
| 187 | DigitsOut = CountryCode ++ DigitsIn, |
| 188 | NatureOut = ?ISUP_ADDR_NAT_INTERNATIONAL, |
| 189 | io:format("Internationalize: ~p -> ~p~n", [DigitsIn, DigitsOut]); |
| 190 | _ -> |
| 191 | DigitsOut = DigitsIn, |
| 192 | NatureOut = Nature |
| 193 | end, |
| 194 | PartyNum#party_number{phone_number = DigitsOut, nature_of_addr_ind = NatureOut}. |
Harald Welte | d25a454 | 2011-01-21 22:42:45 +0100 | [diff] [blame] | 195 | |
| 196 | isup_party_nationalize(PartyNum, CountryCode) -> |
| 197 | #party_number{phone_number = DigitsIn, nature_of_addr_ind = Nature} = PartyNum, |
| 198 | CountryCodeLen = length(CountryCode), |
| 199 | case Nature of |
| 200 | ?ISUP_ADDR_NAT_INTERNATIONAL -> |
| 201 | Pfx = lists:sublist(DigitsIn, CountryCodeLen), |
| 202 | if Pfx == CountryCode -> |
| 203 | DigitsOut = lists:sublist(DigitsIn, CountryCodeLen+1, |
| 204 | length(DigitsIn)-CountryCodeLen), |
| 205 | NatureOut = ?ISUP_ADDR_NAT_NATIONAL, |
| 206 | io:format("Nationalize: ~p -> ~p~n", [DigitsIn, DigitsOut]); |
| 207 | true -> |
| 208 | DigitsOut = DigitsIn, |
| 209 | NatureOut = Nature |
| 210 | end; |
| 211 | _ -> |
| 212 | DigitsOut = DigitsIn, |
| 213 | NatureOut = Nature |
| 214 | end, |
| 215 | PartyNum#party_number{phone_number = DigitsOut, nature_of_addr_ind = NatureOut}. |