A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipcs-classifier-record.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007,2008, 2009 INRIA, UDcast
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19  */
20 #include "ipcs-classifier-record.h"
21 #include <stdint.h>
22 #include "ns3/ipv4-address.h"
23 #include "wimax-tlv.h"
24 NS_LOG_COMPONENT_DEFINE ("IpcsClassifierRecord");
25 
26 namespace ns3 {
27 
28 IpcsClassifierRecord::IpcsClassifierRecord (void)
29 {
30  m_priority = 255;
31  m_priority = 0;
32  m_index = 0;
33  m_tosLow = 0;
34  m_tosHigh = 0;
35  m_tosMask = 0;
36  m_cid = 0;
37  m_protocol.push_back (6); // tcp
38  m_protocol.push_back (17); // udp
39  AddSrcAddr (Ipv4Address ("0.0.0.0"), Ipv4Mask ("0.0.0.0"));
40  AddDstAddr (Ipv4Address ("0.0.0.0"), Ipv4Mask ("0.0.0.0"));
41  AddSrcPortRange (0, 65535);
42  AddDstPortRange (0, 65535);
43 }
44 
45 IpcsClassifierRecord::~IpcsClassifierRecord (void)
46 {
47 }
48 
49 IpcsClassifierRecord::IpcsClassifierRecord (Tlv tlv)
50 {
51  NS_ASSERT_MSG (tlv.GetType () == CsParamVectorTlvValue::Packet_Classification_Rule, "Invalid TLV");
53  m_priority = 0;
54  m_index = 0;
55  m_tosLow = 0;
56  m_tosHigh = 0;
57  m_tosMask = 0;
58  m_cid = 0;
59  for (std::vector<Tlv*>::const_iterator iter = rules->Begin (); iter != rules->End (); ++iter)
60  {
61  switch ((*iter)->GetType ())
62  {
63  case ClassificationRuleVectorTlvValue::Priority:
64  {
65  m_priority = ((U8TlvValue*)((*iter)->PeekValue ()))->GetValue ();
66  break;
67  }
68  case ClassificationRuleVectorTlvValue::ToS:
69  {
70  NS_FATAL_ERROR ("ToS Not implemented-- please implement and contribute a patch");
71  break;
72  }
73  case ClassificationRuleVectorTlvValue::Protocol:
74  {
75  ProtocolTlvValue * list = (ProtocolTlvValue *)(*iter)->PeekValue ();
76  for (std::vector<uint8_t>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
77  {
78  AddProtocol (*iter2);
79  }
80  break;
81  }
82  case ClassificationRuleVectorTlvValue::IP_src:
83  {
84  Ipv4AddressTlvValue * list = (Ipv4AddressTlvValue *)(*iter)->PeekValue ();
85  for (std::vector<Ipv4AddressTlvValue::ipv4Addr>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
86  {
87  AddSrcAddr ((*iter2).Address, (*iter2).Mask);
88  }
89  break;
90  }
91  case ClassificationRuleVectorTlvValue::IP_dst:
92  {
93  Ipv4AddressTlvValue * list = (Ipv4AddressTlvValue *)(*iter)->PeekValue ();
94  for (std::vector<Ipv4AddressTlvValue::ipv4Addr>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
95  {
96  AddDstAddr ((*iter2).Address, (*iter2).Mask);
97  }
98  break;
99  }
100  case ClassificationRuleVectorTlvValue::Port_src:
101  {
102  PortRangeTlvValue * list = (PortRangeTlvValue *)(*iter)->PeekValue ();
103  for (std::vector<PortRangeTlvValue::PortRange>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
104  {
105  AddSrcPortRange ((*iter2).PortLow, (*iter2).PortHigh);
106  }
107  break;
108  }
109  case ClassificationRuleVectorTlvValue::Port_dst:
110  {
111  PortRangeTlvValue * list = (PortRangeTlvValue *)(*iter)->PeekValue ();
112  for (std::vector<PortRangeTlvValue::PortRange>::const_iterator iter2 = list->Begin (); iter2 != list->End (); ++iter2)
113  {
114  AddDstPortRange ((*iter2).PortLow, (*iter2).PortHigh);
115  }
116  break;
117  }
118  case ClassificationRuleVectorTlvValue::Index:
119  {
120  m_index = ((U16TlvValue*)((*iter)->PeekValue ()))->GetValue ();
121  break;
122  }
123  }
124  }
125 }
126 
127 IpcsClassifierRecord::IpcsClassifierRecord (Ipv4Address SrcAddress,
128  Ipv4Mask SrcMask,
129  Ipv4Address DstAddress,
130  Ipv4Mask DstMask,
131  uint16_t SrcPortLow,
132  uint16_t SrcPortHigh,
133  uint16_t DstPortLow,
134  uint16_t DstPortHigh,
135  uint8_t protocol,
136  uint8_t priority)
137 {
138  m_priority = priority;
139  m_protocol.push_back (protocol);
140  AddSrcAddr (SrcAddress, SrcMask);
141  AddDstAddr (DstAddress, DstMask);
142  AddSrcPortRange (SrcPortLow, SrcPortHigh);
143  AddDstPortRange (DstPortLow, DstPortHigh);
144  m_index = 0;
145  m_tosLow = 0;
146  m_tosHigh = 0;
147  m_tosMask = 0;
148  m_cid = 0;
149 }
150 
151 void
153 {
154  struct ipv4Addr tmp;
155  tmp.Address = srcAddress;
156  tmp.Mask = srcMask;
157  m_srcAddr.push_back (tmp);
158 }
159 void
161 {
162  struct ipv4Addr tmp;
163  tmp.Address = dstAddress;
164  tmp.Mask = dstMask;
165  m_dstAddr.push_back (tmp);
166 }
167 void
168 IpcsClassifierRecord::AddSrcPortRange (uint16_t srcPortLow, uint16_t srcPortHigh)
169 {
170  struct PortRange tmp;
171  tmp.PortLow = srcPortLow;
172  tmp.PortHigh = srcPortHigh;
173  m_srcPortRange.push_back (tmp);
174 
175 }
176 void
177 IpcsClassifierRecord::AddDstPortRange (uint16_t dstPortLow, uint16_t dstPortHigh)
178 {
179  struct PortRange tmp;
180  tmp.PortLow = dstPortLow;
181  tmp.PortHigh = dstPortHigh;
182  m_dstPortRange.push_back (tmp);
183 }
184 void
186 {
187  m_protocol.push_back (proto);
188 }
189 void
191 {
192  m_priority = prio;
193 }
194 void
196 {
197  m_cid = cid;
198 }
199 void
201 {
202  m_index = index;
203 }
204 
205 uint16_t
207 {
208  return m_index;
209 }
210 uint16_t
212 {
213  return m_cid;
214 }
215 uint8_t
217 {
218  return m_priority;
219 }
220 
221 bool
222 IpcsClassifierRecord::CheckMatchSrcAddr (Ipv4Address srcAddress) const
223 {
224  for (std::vector<struct ipv4Addr>::const_iterator iter = m_srcAddr.begin (); iter != m_srcAddr.end (); ++iter)
225  {
226  NS_LOG_INFO ("src addr check match: pkt=" << srcAddress << " cls=" << (*iter).Address << "/" << (*iter).Mask);
227  if (srcAddress.CombineMask ((*iter).Mask) == (*iter).Address)
228  {
229  return true;
230  }
231  }
232  NS_LOG_INFO ("NOT OK!");
233  return false;
234 }
235 bool
236 IpcsClassifierRecord::CheckMatchDstAddr (Ipv4Address dstAddress) const
237 {
238 
239  for (std::vector<struct ipv4Addr>::const_iterator iter = m_dstAddr.begin (); iter != m_dstAddr.end (); ++iter)
240  {
241  NS_LOG_INFO ("dst addr check match: pkt=" << dstAddress << " cls=" << (*iter).Address << "/" << (*iter).Mask);
242  if (dstAddress.CombineMask ((*iter).Mask) == (*iter).Address)
243  {
244  return true;
245  }
246  }
247  NS_LOG_INFO ("NOT OK!");
248  return false;
249 }
250 bool
251 IpcsClassifierRecord::CheckMatchSrcPort (uint16_t port) const
252 {
253  for (std::vector<struct PortRange>::const_iterator iter = m_srcPortRange.begin (); iter != m_srcPortRange.end (); ++iter)
254  {
255  NS_LOG_INFO ("src port check match: pkt=" << port << " cls= [" << (*iter).PortLow << " TO " << (*iter).PortHigh
256  << "]");
257  if (port >= (*iter).PortLow && port <= (*iter).PortHigh)
258  {
259  return true;
260  }
261  }
262  NS_LOG_INFO ("NOT OK!");
263  return false;
264 }
265 bool
266 IpcsClassifierRecord::CheckMatchDstPort (uint16_t port) const
267 {
268  for (std::vector<struct PortRange>::const_iterator iter = m_dstPortRange.begin (); iter != m_dstPortRange.end (); ++iter)
269  {
270  NS_LOG_INFO ("dst port check match: pkt=" << port << " cls= [" << (*iter).PortLow << " TO " << (*iter).PortHigh
271  << "]");
272  if (port >= (*iter).PortLow && port <= (*iter).PortHigh)
273  {
274  return true;
275  }
276  }
277  NS_LOG_INFO ("NOT OK!");
278  return false;
279 }
280 bool
281 IpcsClassifierRecord::CheckMatchProtocol (uint8_t proto) const
282 {
283  for (std::vector<uint8_t>::const_iterator iter = m_protocol.begin (); iter != m_protocol.end (); ++iter)
284  {
285  NS_LOG_INFO ("proto check match: pkt=" << (uint16_t) proto << " cls=" << (uint16_t) proto);
286  if (proto == (*iter))
287  {
288  return true;
289  }
290  }
291  NS_LOG_INFO ("NOT OK!");
292  return false;
293 }
294 bool
296  Ipv4Address dstAddress,
297  uint16_t srcPort,
298  uint16_t dstPort,
299  uint8_t proto) const
300 {
301  return (CheckMatchProtocol (proto) && CheckMatchDstPort (dstPort) && CheckMatchSrcPort (srcPort)
302  && CheckMatchDstAddr (dstAddress) && CheckMatchSrcAddr (srcAddress));
303 }
304 
305 Tlv
307 {
308  Ipv4AddressTlvValue ipv4AddrValSrc;
309  for (std::vector<struct ipv4Addr>::const_iterator iter = m_srcAddr.begin (); iter != m_srcAddr.end (); ++iter)
310  {
311  ipv4AddrValSrc.Add ((*iter).Address, (*iter).Mask);
312  }
313 
314  Ipv4AddressTlvValue ipv4AddrValDst;
315  for (std::vector<struct ipv4Addr>::const_iterator iter = m_dstAddr.begin (); iter != m_dstAddr.end (); ++iter)
316  {
317  ipv4AddrValDst.Add ((*iter).Address, (*iter).Mask);
318  }
319 
320  ProtocolTlvValue protoVal;
321  for (std::vector<uint8_t>::const_iterator iter = m_protocol.begin (); iter != m_protocol.end (); ++iter)
322  {
323  protoVal.Add ((*iter));
324  }
325 
326  PortRangeTlvValue portValueSrc;
327  for (std::vector<struct PortRange>::const_iterator iter = m_srcPortRange.begin (); iter != m_srcPortRange.end (); ++iter)
328  {
329  portValueSrc.Add ((*iter).PortLow, (*iter).PortHigh);
330  }
331 
332  PortRangeTlvValue portValueDst;
333  for (std::vector<struct PortRange>::const_iterator iter = m_dstPortRange.begin (); iter != m_dstPortRange.end (); ++iter)
334  {
335  portValueDst.Add ((*iter).PortLow, (*iter).PortHigh);
336  }
337 
339  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::Priority, 1, U8TlvValue (m_priority)));
340  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::Protocol, protoVal.GetSerializedSize (), protoVal));
341  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::IP_src, ipv4AddrValSrc.GetSerializedSize (), ipv4AddrValSrc));
342  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::IP_dst, ipv4AddrValDst.GetSerializedSize (), ipv4AddrValDst));
343  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::Port_src, portValueSrc.GetSerializedSize (), portValueSrc));
344  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::Port_dst, portValueDst.GetSerializedSize (), portValueDst));
345  ClassVectVal.Add (Tlv (ClassificationRuleVectorTlvValue::Index, 2, U16TlvValue (1)));
346 
347  Tlv tmp_tlv (CsParamVectorTlvValue::Packet_Classification_Rule, ClassVectVal.GetSerializedSize (), ClassVectVal);
348 
349  return tmp_tlv;
350 }
351 
352 } // namespace ns3
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
bool CheckMatch(Ipv4Address srcAddress, Ipv4Address dstAddress, uint16_t srcPort, uint16_t dstPort, uint8_t proto) const
check if a packets can be used with this classifier
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
#define NS_LOG_INFO(msg)
Definition: log.h:264
This class implements the Type-Len-Value structure channel encodings as described by "IEEE Standard f...
Definition: wimax-tlv.h:64
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
void AddSrcAddr(Ipv4Address srcAddress, Ipv4Mask srcMask)
add a new source ip address to the classifier
void AddSrcPortRange(uint16_t srcPortLow, uint16_t srcPortHigh)
add a range of source port to the classifier
void AddDstAddr(Ipv4Address dstAddress, Ipv4Mask dstMask)
add a new destination ip address to the classifier
void SetCid(uint16_t cid)
Set the cid associated to this classifier.
void SetPriority(uint8_t prio)
Set the priority of this classifier.
this class implements the classifier descriptor as a tlv vector
Definition: wimax-tlv.h:255
void AddDstPortRange(uint16_t dstPortLow, uint16_t dstPortHigh)
add a range of destination port to the classifier
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void AddProtocol(uint8_t proto)
add a protocol to the classifier
void SetIndex(uint16_t index)
Set the index of the classifier.
Tlv ToTlv(void) const
Creates a TLV from this classifier.