aboutsummaryrefslogtreecommitdiffstats
path: root/src/sccp_masq.erl
blob: eb9ed0dff7ee2f8b5d3d9f7d6a8e7a8613381eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
% ITU-T Q.71x SCCP UDT stateful masquerading

% (C) 2011 by Harald Welte <laforge@gnumonks.org>
%
% All Rights Reserved
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU Affero General Public License as
% published by the Free Software Foundation; either version 3 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU Affero General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

-module(sccp_masq).
-author('Harald Welte <laforge@gnumonks.org>').
-include_lib("osmo_ss7/include/sccp.hrl").

-export([sccp_masq_msg/3, init/0, reset/0, dump/0, lookup_masq_addr/2]).

-compile([export_all]).

-record(sccp_masq_rec, {
	  digits_in,	% list of GT digits
	  digits_out,	% list of GT digits
	  last_access	% timestamp of last usage
	}).

% alloc + insert a new masquerade state record in our tables
masq_alloc(DigitsOrig) ->
	{ok, Base} = application:get_env(mgw_nat, sccp_masq_gt_base),
	{ok, Max} = application:get_env(mgw_nat, sccp_masq_gt_max),
	masq_try_alloc(DigitsOrig, Base, Max, 0).
masq_try_alloc(_DigitsOrig, _Base, Max, Offset) when Offset > Max ->
	undef;
masq_try_alloc(DigitsOrig, Base, Max, Offset) ->
	Try = Base + Offset,
	TryBin = osmo_util:int2digit_list(Try),
	{ok, RevTbl} = application:get_env(mgw_nat, sccp_masq_rev),
	{ok, OrigTbl} = application:get_env(mgw_nat, sccp_masq_orig),
	% try to first allocate the reverse mapping, i.e. where the new
	% masqueraded address is the unique criteria for table lookup
	EtsRet = ets:insert_new(RevTbl,
				#sccp_masq_rec{digits_in = TryBin,
					       digits_out = DigitsOrig}),
	case EtsRet of
		false ->
			masq_try_alloc(DigitsOrig, Base, Max, Offset+1);
		_ ->
			ets:insert(OrigTbl,
				   #sccp_masq_rec{digits_in = DigitsOrig,
						  digits_out = TryBin}),
			Try
	end.

% lookup a masqerade state record
lookup_masq_addr(orig, GtDigits) ->
	{ok, OrigTbl} = application:get_env(mgw_nat, sccp_masq_orig),
	case ets:lookup(OrigTbl, GtDigits) of
		[#sccp_masq_rec{digits_out = DigitsOut}] ->
			DigitsOut;
		_ ->
			% we do not allocate entries right here
			undef
	end;
lookup_masq_addr(rev, GtDigits) ->
	{ok, RevTbl} = application:get_env(mgw_nat, sccp_masq_rev),
	case ets:lookup(RevTbl, GtDigits) of
		[#sccp_masq_rec{digits_out = DigitsOut}] ->
			DigitsOut;
		_ ->
			% we do not allocate entries in the reverse direction
			undef
	end.

lookup_or_alloc_masq_addr(Dir, GtDigits) ->
	case lookup_masq_addr(Dir, GtDigits) of
		undef ->
			% allocate a new masq GT
			masq_alloc(GtDigits);
		Addr ->
			Addr
	end.

% Masquerade the CALLING address in first STP(G-MSC) -> HLR/VLR/MSC dir
mangle_rx_calling(from_stp, Addr = #sccp_addr{global_title = GT}) ->
	GtOrig = GT#global_title.phone_number,
	GtReplace = lookup_or_alloc_masq_addr(orig, GtOrig),
	case GtReplace of
		undef ->
			io:format("SCCP MASQ: Unable to rewrite in original direction (out of GT addrs?)~n"),
			Addr;
		_ ->
			io:format("SCCP MASQ (STP->MSC) rewrite ~p->~p~n", [GtOrig, GtReplace]),
			GTout = GT#global_title{phone_number = GtReplace},
			Addr#sccp_addr{global_title = GTout}
	end;
mangle_rx_calling(_From, Addr) ->
	Addr.

mangle_rx_called(from_msc, Addr = #sccp_addr{global_title = GT}) ->
	GtOrig = GT#global_title.phone_number,
	GtReplace = lookup_masq_addr(rev, GtOrig),
	case GtReplace of
		undef ->
			io:format("SCCP MASQ: Unable to rewrite in original direction (unknown GT ~p)~n", [GT]),
			Addr;
		_ ->
			io:format("SCCP MASQ (MSC->STP) rewrite ~p->~p~n", [GtOrig, GtReplace]),
			GTout = GT#global_title{phone_number = GtReplace},
			Addr#sccp_addr{global_title = GTout}
	end;
mangle_rx_called(_From, Addr) ->
	Addr.


sccp_masq_msg(From, ?SCCP_MSGT_UDT, Msg = #sccp_msg{parameters = Opts}) ->
	CalledParty = proplists:get_value(called_party_addr, Opts),
	CalledPartyNew = mangle_rx_called(From, CalledParty),
	CallingParty = proplists:get_value(calling_party_addr, Opts),
	CallingPartyNew = mangle_rx_calling(From, CallingParty),
	Opts1 = lists:keyreplace(called_party_addr, 1, Opts,
				 {called_party_addr, CalledPartyNew}),
	Opts2 = lists:keyreplace(calling_party_addr, 1, Opts1,
				 {calling_party_addr, CallingPartyNew}),
	Msg#sccp_msg{parameters = Opts2};
sccp_masq_msg(_From, _MsgType, Msg) ->
	Msg.

init() ->
	Orig = ets:new(sccp_masq_orig, [ordered_set, public,
					{keypos, #sccp_masq_rec.digits_in}]),
	Rev  = ets:new(sccp_masq_rev, [ordered_set, public,
					{keypos, #sccp_masq_rec.digits_in}]),
	application:set_env(mgw_nat, sccp_masq_orig, Orig),
	application:set_env(mgw_nat, sccp_masq_rev, Rev),
	ok.

reset() ->
	io:format("SCCP MASQ: Deleting all MASQ state records~n"),
	{ok, OrigTbl} = application:get_env(mgw_nat, sccp_masq_orig),
	{ok, RevTbl} = application:get_env(mgw_nat, sccp_masq_rev),
	ets:delete_all_objects(OrigTbl),
	ets:delete_all_objects(RevTbl).

dump() ->
	{ok, OrigTbl} = application:get_env(mgw_nat, sccp_masq_orig),
	{ok, RevTbl} = application:get_env(mgw_nat, sccp_masq_rev),
	ListOrig = ets:tab2list(OrigTbl),
	ListRev = ets:tab2list(RevTbl),
	io:format("SCCP MASQ Table Dump (ORIGINAL)~n"),
	dump_list(ListOrig),
	io:format("SCCP MASQ Table Dump (REVERSE)~n"),
	dump_list(ListRev).

dump_list([]) ->
	ok;
dump_list([#sccp_masq_rec{digits_in=DigIn, digits_out=DigOut}|Tail]) ->
	DigInInt = osmo_util:digit_list2int(DigIn),
	DigOutInt = osmo_util:digit_list2int(DigOut),
	io:format("~p -> ~p~n", [DigInInt, DigOutInt]),
	dump_list(Tail).