blob: b35b763f19482af0242624fb2cd6c1d852f4abd9 [file] [log] [blame]
Harald Welteaca4edc2011-01-21 16:21:12 +00001%
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
34mangle_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
50mangle_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
63mangle_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
Harald Welteed01ec42011-01-22 20:54:36 +000067mangle_rx_mtp3_serv(_L, From, ?MTP3_SERV_ISUP, Mtp3 = #mtp3_msg{payload = Payload}) ->
Harald Welteaca4edc2011-01-21 16:21:12 +000068 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 Welte49ec10e2011-01-21 17:21:13 +000072 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 Welteaca4edc2011-01-21 16:21:12 +000081 end;
82% mangle the SCCP content
Harald Welte66517c02011-02-03 14:12:50 +010083mangle_rx_mtp3_serv(_L, From, ?MTP3_SERV_SCCP, Mtp3 = #mtp3_msg{payload = Payload}) ->
84 io:format("SCCP In: ~p~n", [Payload]),
85 {ok, Sccp} = sccp_codec:parse_sccp_msg(Payload),
Harald Welteaca4edc2011-01-21 16:21:12 +000086 io:format("SCCP Decode: ~p~n", [Sccp]),
Harald Welte66517c02011-02-03 14:12:50 +010087 SccpMangled = mangle_rx_sccp(From, Sccp#sccp_msg.msg_type, Sccp),
88 if SccpMangled == Sccp ->
89 Mtp3;
90 true ->
91 io:format("SCCP Encode In: ~p~n", [SccpMangled]),
92 Payload_out = sccp_codec:encode_sccp_msg(SccpMangled),
93 io:format("SCCP Encode Out: ~p~n", [Payload_out]),
94 % return modified MTP3 payload
95 Mtp3#mtp3_msg{payload = Payload_out}
96 end;
Harald Welteaca4edc2011-01-21 16:21:12 +000097% default: do nothing
98mangle_rx_mtp3_serv(_L, _From, _, Mtp3) ->
99 Mtp3.
100
Harald Welte66517c02011-02-03 14:12:50 +0100101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102% Actual mangling of the decoded SCCP messages
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Harald Welte918c1f22011-02-03 15:55:57 +0100104
Harald Welte918c1f22011-02-03 15:55:57 +0100105mangle_rx_called(from_stp, Addr = #sccp_addr{ssn = SSN,
106 global_title = GT}) ->
Harald Welte790f3642011-02-03 17:44:50 +0100107 {ok, RealHlrGt} = application:get_env(real_hlr_gt),
108 {ok, NatHlrGt} = application:get_env(nat_hlr_gt),
Harald Welte918c1f22011-02-03 15:55:57 +0100109 case {SSN, GT#global_title.phone_number} of
Harald Welte790f3642011-02-03 17:44:50 +0100110 {?SCCP_SSN_HLR, RealHlrGt} ->
111 GTout = GT#global_title{phone_number = NatHlrGt},
Harald Welte918c1f22011-02-03 15:55:57 +0100112 io:format("SCCP STP->MSC rewrite ~p~n", [GTout]),
113 Addr#sccp_addr{global_title = GTout};
114 _ ->
115 Addr
116 end;
117mangle_rx_called(_From, Addr) ->
118 Addr.
119
120mangle_rx_calling(from_msc, Addr = #sccp_addr{ssn = SSN,
121 global_title = GT}) ->
Harald Welte790f3642011-02-03 17:44:50 +0100122 {ok, RealHlrGt} = application:get_env(real_hlr_gt),
123 {ok, NatHlrGt} = application:get_env(nat_hlr_gt),
Harald Welte918c1f22011-02-03 15:55:57 +0100124 case {SSN, GT#global_title.phone_number} of
Harald Welte790f3642011-02-03 17:44:50 +0100125 {?SCCP_SSN_MSC, NatHlrGt} ->
126 GTout = GT#global_title{phone_number = RealHlrGt},
Harald Welte918c1f22011-02-03 15:55:57 +0100127 io:format("SCCP MSC->STP rewrite ~p~n", [GTout]),
128 Addr#sccp_addr{global_title = GTout};
129 _ ->
130 Addr
131 end;
132mangle_rx_calling(_From, Addr) ->
133 Addr.
134
135mangle_rx_sccp(From, ?SCCP_MSGT_UDT, Msg = #sccp_msg{parameters = Opts}) ->
136 CalledParty = proplists:get_value(called_party_addr, Opts),
137 CalledPartyNew = mangle_rx_called(From, CalledParty),
138 CallingParty = proplists:get_value(calling_party_addr, Opts),
139 CallingPartyNew = mangle_rx_calling(From, CallingParty),
140 Opts1 = lists:keyreplace(called_party_addr, 1, Opts,
141 {called_party_addr, CalledPartyNew}),
142 Opts2 = lists:keyreplace(calling_party_addr, 1, Opts1,
143 {calling_party_addr, CallingPartyNew}),
144 Msg#sccp_msg{parameters = Opts2};
Harald Welte66517c02011-02-03 14:12:50 +0100145mangle_rx_sccp(_From, _MsgType, Msg) ->
Harald Welte66517c02011-02-03 14:12:50 +0100146 Msg.
Harald Welte49ec10e2011-01-21 17:21:13 +0000147
148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149% Actual mangling of the decoded ISUP messages
150%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
Harald Welte2fc3e982011-01-21 18:50:36 +0000152% iterate over list of parameters and call mangle_rx_isup_par() for each one
153mangle_rx_isup_params(_From, _MsgType, _Msg, ParListOut, []) ->
154 ParListOut;
155mangle_rx_isup_params(From, MsgType, Msg, ParListOut, [Par|ParList]) ->
156 ParOut = mangle_rx_isup_par(From, MsgType, Msg, Par),
157 mangle_rx_isup_params(From, MsgType, Msg, ParListOut++[ParOut], ParList).
158
159% manipulate phone numbers
Harald Welteed01ec42011-01-22 20:54:36 +0000160mangle_rx_isup_par(From, MsgType, _Msg, {ParType, ParBody}) when
Harald Welte2fc3e982011-01-21 18:50:36 +0000161 ParType == ?ISUP_PAR_CALLED_P_NUM;
Harald Welteed01ec42011-01-22 20:54:36 +0000162 ParType == ?ISUP_PAR_CONNECTED_NUM;
Harald Welte2fc3e982011-01-21 18:50:36 +0000163 ParType == ?ISUP_PAR_CALLING_P_NUM ->
164 NewParBody = mangle_isup_number(From, MsgType, ParType, ParBody),
165 {ParType, NewParBody};
166% defauly case: do not mangle this parameter
167mangle_rx_isup_par(_From, _MsgType, _Msg, Par) ->
168 Par.
169
170% mangle an incoming ISUP message
171mangle_rx_isup(From, MsgType, Msg = #isup_msg{parameters = Params}) ->
172 ParamsOut = mangle_rx_isup_params(From, MsgType, Msg, [], Params),
173 % return message with modified parameter list
174 Msg#isup_msg{parameters = ParamsOut}.
Harald Welte49ec10e2011-01-21 17:21:13 +0000175
Harald Welte62469be2011-01-21 22:47:42 +0100176% STP->MSC: Mangle a Party Number in IAM
Harald Welte49ec10e2011-01-21 17:21:13 +0000177mangle_isup_number(from_stp, ?ISUP_MSGT_IAM, NumType, PartyNum) ->
178 case NumType of
179 ?ISUP_PAR_CALLED_P_NUM ->
Harald Weltef62f0ed2011-01-22 10:13:34 +0100180 % First convert to international number, if it is national
Harald Welte790f3642011-02-03 17:44:50 +0100181 Num1 = isup_party_internationalize(PartyNum,
182 application:get_env(intern_pfx)),
Harald Welte49ec10e2011-01-21 17:21:13 +0000183 io:format("IAM MSRN rewrite (STP->MSC): "),
Harald Welte790f3642011-02-03 17:44:50 +0100184 isup_party_replace_prefix(Num1,
185 application:get_env(msrn_pfx_stp),
186 application:get_env(msrn_pfx_msc));
Harald Welte49ec10e2011-01-21 17:21:13 +0000187 _ ->
188 PartyNum
Harald Welteacb8c2c2011-01-21 17:26:53 +0000189 end;
Harald Welte62469be2011-01-21 22:47:42 +0100190% MSC->STP: Mangle connected number in response to IAM
Harald Welte93443542011-01-21 18:56:58 +0000191mangle_isup_number(from_msc, MsgT, NumType, PartyNum) when MsgT == ?ISUP_MSGT_CON;
192 MsgT == ?ISUP_MSGT_ANM ->
193 case NumType of
194 ?ISUP_PAR_CONNECTED_NUM ->
195 io:format("CON MSRN rewrite (MSC->STP): "),
Harald Welte790f3642011-02-03 17:44:50 +0100196 Num1 = isup_party_replace_prefix(PartyNum,
197 application:get_env(msrn_pfx_msc),
198 application:get_env(msrn_pfx_stp)),
Harald Weltef62f0ed2011-01-22 10:13:34 +0100199 % Second: convert to national number, if it is international
Harald Welte790f3642011-02-03 17:44:50 +0100200 isup_party_nationalize(Num1,
201 application:get_env(intern_pfx));
Harald Welte93443542011-01-21 18:56:58 +0000202 _ ->
203 PartyNum
204 end;
Harald Welte62469be2011-01-21 22:47:42 +0100205% MAC->STP: Mangle IAM international -> national
Harald Welted25a4542011-01-21 22:42:45 +0100206mangle_isup_number(from_msc, ?ISUP_MSGT_IAM, NumType, PartyNum) ->
207 case NumType of
208 ?ISUP_PAR_CALLED_P_NUM ->
Harald Welte790f3642011-02-03 17:44:50 +0100209 isup_party_nationalize(PartyNum,
210 applicaiton:get_env(intern_pfx));
Harald Welted25a4542011-01-21 22:42:45 +0100211 _ ->
212 PartyNum
213 end;
Harald Welte62469be2011-01-21 22:47:42 +0100214% STP->MSC: Mangle connected number in response to IAM (national->international)
215mangle_isup_number(from_stp, MsgT, NumType, PartyNum) when MsgT == ?ISUP_MSGT_CON;
216 MsgT == ?ISUP_MSGT_ANM ->
217 case NumType of
218 ?ISUP_PAR_CONNECTED_NUM ->
Harald Welte790f3642011-02-03 17:44:50 +0100219 isup_party_internationalize(PartyNum,
220 application:get_env(intern_pfx));
Harald Welte62469be2011-01-21 22:47:42 +0100221 _ ->
222 PartyNum
223 end;
Harald Welteacb8c2c2011-01-21 17:26:53 +0000224% default case: no rewrite
225mangle_isup_number(from_msc, _, _, PartyNum) ->
226 PartyNum.
Harald Welte49ec10e2011-01-21 17:21:13 +0000227
228% replace the prefix of PartyNum with NewPfx _if_ the current prefix matches MatchPfx
Harald Welte7871c752011-01-21 19:25:23 +0000229isup_party_replace_prefix(PartyNum, MatchPfx, NewPfx) ->
Harald Welte49ec10e2011-01-21 17:21:13 +0000230 DigitsIn = PartyNum#party_number.phone_number,
231 MatchPfxLen = length(MatchPfx),
232 Pfx = lists:sublist(DigitsIn, 1, MatchPfxLen),
233 if Pfx == MatchPfx ->
Harald Welte1df3c552011-01-21 17:33:45 +0000234 Trailer = lists:sublist(DigitsIn, MatchPfxLen+1, length(DigitsIn)-MatchPfxLen),
Harald Welte49ec10e2011-01-21 17:21:13 +0000235 DigitsOut = NewPfx ++ Trailer,
Harald Welte7871c752011-01-21 19:25:23 +0000236 io:format("Prefix rewrite: ~p -> ~p~n", [DigitsIn, DigitsOut]);
Harald Welte49ec10e2011-01-21 17:21:13 +0000237 true ->
Harald Welte7871c752011-01-21 19:25:23 +0000238 io:format("Prefix rewrite: NO MATCH (~p != ~p)~n", [Pfx, MatchPfx]),
Harald Welte49ec10e2011-01-21 17:21:13 +0000239 DigitsOut = DigitsIn
240 end,
241 PartyNum#party_number{phone_number = DigitsOut}.
Harald Welte7871c752011-01-21 19:25:23 +0000242
243isup_party_internationalize(PartyNum, CountryCode) ->
244 #party_number{phone_number = DigitsIn, nature_of_addr_ind = Nature} = PartyNum,
245 case Nature of
246 ?ISUP_ADDR_NAT_NATIONAL ->
247 DigitsOut = CountryCode ++ DigitsIn,
248 NatureOut = ?ISUP_ADDR_NAT_INTERNATIONAL,
249 io:format("Internationalize: ~p -> ~p~n", [DigitsIn, DigitsOut]);
250 _ ->
251 DigitsOut = DigitsIn,
252 NatureOut = Nature
253 end,
254 PartyNum#party_number{phone_number = DigitsOut, nature_of_addr_ind = NatureOut}.
Harald Welted25a4542011-01-21 22:42:45 +0100255
256isup_party_nationalize(PartyNum, CountryCode) ->
257 #party_number{phone_number = DigitsIn, nature_of_addr_ind = Nature} = PartyNum,
258 CountryCodeLen = length(CountryCode),
259 case Nature of
260 ?ISUP_ADDR_NAT_INTERNATIONAL ->
261 Pfx = lists:sublist(DigitsIn, CountryCodeLen),
262 if Pfx == CountryCode ->
263 DigitsOut = lists:sublist(DigitsIn, CountryCodeLen+1,
264 length(DigitsIn)-CountryCodeLen),
265 NatureOut = ?ISUP_ADDR_NAT_NATIONAL,
266 io:format("Nationalize: ~p -> ~p~n", [DigitsIn, DigitsOut]);
267 true ->
268 DigitsOut = DigitsIn,
269 NatureOut = Nature
270 end;
271 _ ->
272 DigitsOut = DigitsIn,
273 NatureOut = Nature
274 end,
275 PartyNum#party_number{phone_number = DigitsOut, nature_of_addr_ind = NatureOut}.