forked from nsnam/ns-3-dev-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepc-tft.cc
305 lines (282 loc) · 9.11 KB
/
epc-tft.cc
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2011 CTTC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Nicola Baldo <[email protected]>
*/
#include "epc-tft.h"
#include "ns3/abort.h"
#include "ns3/log.h"
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("EpcTft");
/**
* Output stream operator for EPC TFT direction
*
* \param os output stream
* \param d EPC TFT direction
* \return ostream
*/
std::ostream& operator<< (std::ostream& os, EpcTft::Direction& d)
{
switch (d)
{
case EpcTft::DOWNLINK:
os << "DOWNLINK";
break;
case EpcTft::UPLINK:
os << "UPLINK";
break;
default:
os << "BIDIRECTIONAL";
break;
}
return os;
}
/**
* Output stream for EPC TFT packet filter
*
* \param os output stream
* \param f EPC TFT packet filter
* \return ostream
*/
std::ostream& operator<< (std::ostream& os, EpcTft::PacketFilter& f)
{
os << " direction: " << f.direction
<< " remoteAddress: " << f.remoteAddress
<< " remoteMask: " << f.remoteMask
<< " remoteIpv6Address: " << f.remoteIpv6Address
<< " remoteIpv6Prefix: " << f.remoteIpv6Prefix
<< " localAddress: " << f.localAddress
<< " localMask: " << f.localMask
<< " localIpv6Address: " << f.localIpv6Address
<< " localIpv6Prefix: " << f.localIpv6Prefix
<< " remotePortStart: " << f.remotePortStart
<< " remotePortEnd: " << f.remotePortEnd
<< " localPortStart: " << f.localPortStart
<< " localPortEnd: " << f.localPortEnd
<< " typeOfService: 0x" << std::hex << (uint16_t) f.typeOfService << std::dec
<< " typeOfServiceMask: 0x" << std::hex << (uint16_t) f.typeOfServiceMask << std::dec;
return os;
}
EpcTft::PacketFilter::PacketFilter ()
: precedence (255),
direction (BIDIRECTIONAL),
remoteMask ("0.0.0.0"),
localMask ("0.0.0.0"),
remotePortStart (0),
remotePortEnd (65535),
localPortStart (0),
localPortEnd (65535),
typeOfService (0),
typeOfServiceMask (0)
{
NS_LOG_FUNCTION (this);
}
bool
EpcTft::PacketFilter::Matches (Direction d,
Ipv4Address ra,
Ipv4Address la,
uint16_t rp,
uint16_t lp,
uint8_t tos)
{
NS_LOG_FUNCTION (this << d << ra << la << rp << lp << (uint16_t) tos);
if (d & direction)
{
NS_LOG_LOGIC ("d matches");
if (remoteMask.IsMatch (remoteAddress, ra))
{
NS_LOG_LOGIC ("ra matches");
if (localMask.IsMatch (localAddress, la))
{
NS_LOG_LOGIC ("la matches");
if (remotePortStart <= rp && rp <= remotePortEnd)
{
NS_LOG_LOGIC ("rp matches");
if (localPortStart <= lp && lp <= localPortEnd)
{
NS_LOG_LOGIC ("lp matches");
if ((tos & typeOfServiceMask) == (typeOfService & typeOfServiceMask))
{
NS_LOG_LOGIC ("tos matches --> have match!");
return true;
}
else
{
NS_LOG_LOGIC ("tos doesn't match: tos=" << tos << " f.tos=" << typeOfService << " f.tosmask=" << typeOfServiceMask);
}
}
else
{
NS_LOG_LOGIC ("lp doesn't match: lp=" << lp << " f.lps=" << localPortStart << " f.lpe=" << localPortEnd);
}
}
else
{
NS_LOG_LOGIC ("rp doesn't match: rp=" << rp << " f.rps=" << remotePortStart << " f.lpe=" << remotePortEnd);
}
}
else
{
NS_LOG_LOGIC ("la doesn't match: la=" << la << " f.la=" << localAddress << " f.lmask=" << localMask);
}
}
else
{
NS_LOG_LOGIC ("ra doesn't match: ra=" << ra << " f.ra=" << remoteAddress << " f.rmask=" << remoteMask);
}
}
else
{
NS_LOG_LOGIC ("d doesn't match: d=0x" << std::hex << d << " f.d=0x" << std::hex << direction << std::dec);
}
return false;
}
bool
EpcTft::PacketFilter::Matches (Direction d,
Ipv6Address ra,
Ipv6Address la,
uint16_t rp,
uint16_t lp,
uint8_t tos)
{
NS_LOG_FUNCTION (this << d << ra << la << rp << lp << (uint16_t) tos);
if (d & direction)
{
NS_LOG_LOGIC ("d matches");
if (remoteIpv6Prefix.IsMatch (remoteIpv6Address, ra))
{
NS_LOG_LOGIC ("ra matches");
if (localIpv6Prefix.IsMatch (localIpv6Address, la))
{
NS_LOG_LOGIC ("la matches");
if (remotePortStart <= rp && rp <= remotePortEnd)
{
NS_LOG_LOGIC ("rp matches");
if (localPortStart <= lp && lp <= localPortEnd)
{
NS_LOG_LOGIC ("lp matches");
if ((tos & typeOfServiceMask) == (typeOfService & typeOfServiceMask))
{
NS_LOG_LOGIC ("tos matches --> have match!");
return true;
}
else
{
NS_LOG_LOGIC ("tos doesn't match: tos=" << tos << " f.tos=" << typeOfService << " f.tosmask=" << typeOfServiceMask);
}
}
else
{
NS_LOG_LOGIC ("lp doesn't match: lp=" << lp << " f.lps=" << localPortStart << " f.lpe=" << localPortEnd);
}
}
else
{
NS_LOG_LOGIC ("rp doesn't match: rp=" << rp << " f.rps=" << remotePortStart << " f.lpe=" << remotePortEnd);
}
}
else
{
NS_LOG_LOGIC ("la doesn't match: la=" << la << " f.la=" << localIpv6Address << " f.lprefix=" << localIpv6Prefix);
}
}
else
{
NS_LOG_LOGIC ("ra doesn't match: ra=" << ra << " f.ra=" << remoteIpv6Address << " f.rprefix=" << remoteIpv6Prefix);
}
}
else
{
NS_LOG_LOGIC ("d doesn't match: d=0x" << std::hex << d << " f.d=0x" << std::hex << direction << std::dec);
}
return false;
}
Ptr<EpcTft>
EpcTft::Default ()
{
Ptr<EpcTft> tft = Create<EpcTft> ();
EpcTft::PacketFilter defaultPacketFilter;
tft->Add (defaultPacketFilter);
return tft;
}
EpcTft::EpcTft ()
: m_numFilters (0)
{
NS_LOG_FUNCTION (this);
}
uint8_t
EpcTft::Add (PacketFilter f)
{
NS_LOG_FUNCTION (this << f);
NS_ABORT_IF (m_numFilters >= 16);
std::list<PacketFilter>::iterator it;
for (it = m_filters.begin ();
(it != m_filters.end ()) && (it->precedence <= f.precedence);
++it)
{
}
m_filters.insert (it, f);
++m_numFilters;
return (m_numFilters - 1);
}
bool
EpcTft::Matches (Direction direction,
Ipv4Address remoteAddress,
Ipv4Address localAddress,
uint16_t remotePort,
uint16_t localPort,
uint8_t typeOfService)
{
NS_LOG_FUNCTION (this << direction << remoteAddress << localAddress << std::dec << remotePort << localPort << (uint16_t) typeOfService);
for (std::list<PacketFilter>::iterator it = m_filters.begin ();
it != m_filters.end ();
++it)
{
if (it->Matches (direction, remoteAddress, localAddress, remotePort, localPort, typeOfService))
{
return true;
}
}
return false;
}
bool
EpcTft::Matches (Direction direction,
Ipv6Address remoteAddress,
Ipv6Address localAddress,
uint16_t remotePort,
uint16_t localPort,
uint8_t typeOfService)
{
NS_LOG_FUNCTION (this << direction << remoteAddress << localAddress << std::dec << remotePort << localPort << (uint16_t) typeOfService);
for (std::list<PacketFilter>::iterator it = m_filters.begin ();
it != m_filters.end ();
++it)
{
if (it->Matches (direction, remoteAddress, localAddress, remotePort, localPort, typeOfService))
{
return true;
}
}
return false;
}
std::list<EpcTft::PacketFilter>
EpcTft::GetPacketFilters () const
{
NS_LOG_FUNCTION (this);
return m_filters;
};
} // namespace ns3