5G-LENA nr-v4.0
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
nr-epc-tft.cc
1// Copyright (c) 2011 CTTC
2//
3// SPDX-License-Identifier: GPL-2.0-only
4//
5// Author: Nicola Baldo <nbaldo@cttc.es>
6
7#include "nr-epc-tft.h"
8
9#include "ns3/abort.h"
10#include "ns3/log.h"
11
12namespace ns3
13{
14
15NS_LOG_COMPONENT_DEFINE("NrEpcTft");
16
24std::ostream&
25operator<<(std::ostream& os, const NrEpcTft::Direction& d)
26{
27 switch (d)
28 {
29 case NrEpcTft::DOWNLINK:
30 os << "DOWNLINK";
31 break;
32 case NrEpcTft::UPLINK:
33 os << "UPLINK";
34 break;
35 default:
36 os << "BIDIRECTIONAL";
37 break;
38 }
39 return os;
40}
41
49std::ostream&
50operator<<(std::ostream& os, const NrEpcTft::PacketFilter& f)
51{
52 os << " direction: " << f.direction << " remoteAddress: " << f.remoteAddress
53 << " remoteMask: " << f.remoteMask << " remoteIpv6Address: " << f.remoteIpv6Address
54 << " remoteIpv6Prefix: " << f.remoteIpv6Prefix << " localAddress: " << f.localAddress
55 << " localMask: " << f.localMask << " localIpv6Address: " << f.localIpv6Address
56 << " localIpv6Prefix: " << f.localIpv6Prefix << " remotePortStart: " << f.remotePortStart
57 << " remotePortEnd: " << f.remotePortEnd << " localPortStart: " << f.localPortStart
58 << " localPortEnd: " << f.localPortEnd << " typeOfService: 0x" << std::hex
59 << (uint16_t)f.typeOfService << std::dec << " typeOfServiceMask: 0x" << std::hex
60 << (uint16_t)f.typeOfServiceMask << std::dec;
61 return os;
62}
63
64NrEpcTft::PacketFilter::PacketFilter()
65 : precedence(255),
66 direction(BIDIRECTIONAL),
67 remoteMask("0.0.0.0"),
68 localMask("0.0.0.0"),
69 remotePortStart(0),
70 remotePortEnd(65535),
71 localPortStart(0),
72 localPortEnd(65535),
73 typeOfService(0),
74 typeOfServiceMask(0)
75{
76 NS_LOG_FUNCTION(this);
77}
78
79bool
80NrEpcTft::PacketFilter::Matches(Direction d,
81 Ipv4Address ra,
82 Ipv4Address la,
83 uint16_t rp,
84 uint16_t lp,
85 uint8_t tos)
86{
87 NS_LOG_FUNCTION(this << d << ra << la << rp << lp << (uint16_t)tos);
88 if (d & direction)
89 {
90 NS_LOG_LOGIC("d matches");
91 if (remoteMask.IsMatch(remoteAddress, ra))
92 {
93 NS_LOG_LOGIC("ra matches");
94 if (localMask.IsMatch(localAddress, la))
95 {
96 NS_LOG_LOGIC("la matches");
97 if (remotePortStart <= rp && rp <= remotePortEnd)
98 {
99 NS_LOG_LOGIC("rp matches");
100 if (localPortStart <= lp && lp <= localPortEnd)
101 {
102 NS_LOG_LOGIC("lp matches");
103 if ((tos & typeOfServiceMask) == (typeOfService & typeOfServiceMask))
104 {
105 NS_LOG_LOGIC("tos matches --> have match!");
106 return true;
107 }
108 else
109 {
110 NS_LOG_LOGIC("tos doesn't match: tos="
111 << tos << " f.tos=" << typeOfService
112 << " f.tosmask=" << typeOfServiceMask);
113 }
114 }
115 else
116 {
117 NS_LOG_LOGIC("lp doesn't match: lp=" << lp << " f.lps=" << localPortStart
118 << " f.lpe=" << localPortEnd);
119 }
120 }
121 else
122 {
123 NS_LOG_LOGIC("rp doesn't match: rp=" << rp << " f.rps=" << remotePortStart
124 << " f.lpe=" << remotePortEnd);
125 }
126 }
127 else
128 {
129 NS_LOG_LOGIC("la doesn't match: la=" << la << " f.la=" << localAddress
130 << " f.lmask=" << localMask);
131 }
132 }
133 else
134 {
135 NS_LOG_LOGIC("ra doesn't match: ra=" << ra << " f.ra=" << remoteAddress
136 << " f.rmask=" << remoteMask);
137 }
138 }
139 else
140 {
141 NS_LOG_LOGIC("d doesn't match: d=0x" << std::hex << d << " f.d=0x" << std::hex << direction
142 << std::dec);
143 }
144 return false;
145}
146
147bool
148NrEpcTft::PacketFilter::Matches(Direction d,
149 Ipv6Address ra,
150 Ipv6Address la,
151 uint16_t rp,
152 uint16_t lp,
153 uint8_t tos)
154{
155 NS_LOG_FUNCTION(this << d << ra << la << rp << lp << (uint16_t)tos);
156 if (d & direction)
157 {
158 NS_LOG_LOGIC("d matches");
159 if (remoteIpv6Prefix.IsMatch(remoteIpv6Address, ra))
160 {
161 NS_LOG_LOGIC("ra matches");
162 if (localIpv6Prefix.IsMatch(localIpv6Address, la))
163 {
164 NS_LOG_LOGIC("la matches");
165 if (remotePortStart <= rp && rp <= remotePortEnd)
166 {
167 NS_LOG_LOGIC("rp matches");
168 if (localPortStart <= lp && lp <= localPortEnd)
169 {
170 NS_LOG_LOGIC("lp matches");
171 if ((tos & typeOfServiceMask) == (typeOfService & typeOfServiceMask))
172 {
173 NS_LOG_LOGIC("tos matches --> have match!");
174 return true;
175 }
176 else
177 {
178 NS_LOG_LOGIC("tos doesn't match: tos="
179 << tos << " f.tos=" << typeOfService
180 << " f.tosmask=" << typeOfServiceMask);
181 }
182 }
183 else
184 {
185 NS_LOG_LOGIC("lp doesn't match: lp=" << lp << " f.lps=" << localPortStart
186 << " f.lpe=" << localPortEnd);
187 }
188 }
189 else
190 {
191 NS_LOG_LOGIC("rp doesn't match: rp=" << rp << " f.rps=" << remotePortStart
192 << " f.lpe=" << remotePortEnd);
193 }
194 }
195 else
196 {
197 NS_LOG_LOGIC("la doesn't match: la=" << la << " f.la=" << localIpv6Address
198 << " f.lprefix=" << localIpv6Prefix);
199 }
200 }
201 else
202 {
203 NS_LOG_LOGIC("ra doesn't match: ra=" << ra << " f.ra=" << remoteIpv6Address
204 << " f.rprefix=" << remoteIpv6Prefix);
205 }
206 }
207 else
208 {
209 NS_LOG_LOGIC("d doesn't match: d=0x" << std::hex << d << " f.d=0x" << std::hex << direction
210 << std::dec);
211 }
212 return false;
213}
214
215Ptr<NrEpcTft>
216NrEpcTft::Default()
217{
218 Ptr<NrEpcTft> tft = Create<NrEpcTft>();
219 NrEpcTft::PacketFilter defaultPacketFilter;
220 tft->Add(defaultPacketFilter);
221 return tft;
222}
223
224NrEpcTft::NrEpcTft()
225 : m_numFilters(0)
226{
227 NS_LOG_FUNCTION(this);
228}
229
230uint8_t
232{
233 NS_LOG_FUNCTION(this << f);
234 NS_ABORT_IF(m_numFilters >= 16);
235
236 std::list<PacketFilter>::iterator it;
237 for (it = m_filters.begin(); (it != m_filters.end()) && (it->precedence <= f.precedence); ++it)
238 {
239 }
240 m_filters.insert(it, f);
241 ++m_numFilters;
242 return (m_numFilters - 1);
243}
244
245bool
247 Ipv4Address remoteAddress,
248 Ipv4Address localAddress,
249 uint16_t remotePort,
250 uint16_t localPort,
251 uint8_t typeOfService)
252{
253 NS_LOG_FUNCTION(this << direction << remoteAddress << localAddress << std::dec << remotePort
254 << localPort << (uint16_t)typeOfService);
255 for (auto it = m_filters.begin(); it != m_filters.end(); ++it)
256 {
257 if (it->Matches(direction,
258 remoteAddress,
259 localAddress,
260 remotePort,
261 localPort,
262 typeOfService))
263 {
264 return true;
265 }
266 }
267 return false;
268}
269
270bool
272 Ipv6Address remoteAddress,
273 Ipv6Address localAddress,
274 uint16_t remotePort,
275 uint16_t localPort,
276 uint8_t typeOfService)
277{
278 NS_LOG_FUNCTION(this << direction << remoteAddress << localAddress << std::dec << remotePort
279 << localPort << (uint16_t)typeOfService);
280 for (auto it = m_filters.begin(); it != m_filters.end(); ++it)
281 {
282 if (it->Matches(direction,
283 remoteAddress,
284 localAddress,
285 remotePort,
286 localPort,
287 typeOfService))
288 {
289 return true;
290 }
291 }
292 return false;
293}
294
295std::list<NrEpcTft::PacketFilter>
297{
298 NS_LOG_FUNCTION(this);
299 return m_filters;
300}
301
302} // namespace ns3
std::list< PacketFilter > GetPacketFilters() const
uint8_t Add(PacketFilter f)
bool Matches(Direction direction, Ipv4Address remoteAddress, Ipv4Address localAddress, uint16_t remotePort, uint16_t localPort, uint8_t typeOfService)