1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  *  Copyright (c) 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  */
21 
22 #ifndef WIMAX_TLV_H
23 #define WIMAX_TLV_H
24 
25 #define WIMAX_TLV_EXTENDED_LENGTH_MASK 0x80
26 
27 #include "ns3/ipv4-address.h"
28 #include <cstdlib>
29 #include "ns3/log.h"
30 #include "ns3/assert.h"
31 #include "ns3/uinteger.h"
32 #include "ns3/header.h"
33 #include <vector>
34 
35 namespace ns3 {
36 
37 /**
38  * \ingroup wimax
39  * The value field of a tlv can take different values (uint8_t, uint16,
40  * vector, ...). This class is a virtual interface
41  * from which all the types of tlv values should derive
42  */
43 class TlvValue
44 {
45 public:
~TlvValue()46   virtual ~TlvValue ()
47   {
48   }
49   /**
50    * Get serialized size in bytes
51    * \returns the serialized size
52    */
53   virtual uint32_t GetSerializedSize (void) const = 0;
54   /**
55    * Serialize to a buffer
56    * \param start the iterator
57    */
58   virtual void Serialize (Buffer::Iterator start) const = 0;
59   /**
60    * Deserialize from a buffer
61    * \param start the iterator
62    * \param valueLen the maximum length of the value
63    * \returns the
64    */
65   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLen ) = 0;
66   /**
67    * Copy function
68    * \returns the TLV value
69    */
70   virtual TlvValue * Copy (void) const = 0;
71 private:
72 };
73 
74 
75 // =============================================================================
76 /**
77  * \ingroup wimax
78  * \brief This class implements the Type-Len-Value structure channel encodings as described by "IEEE Standard for
79  * Local and metropolitan area networks Part 16: Air Interface for Fixed Broadband Wireless Access Systems"
80  * 11. TLV encodings, page 645
81  *
82  */
83 class Tlv : public Header
84 {
85 public:
86   /// CommonTypes enumeration
87   enum CommonTypes
88   {
89     HMAC_TUPLE = 149,
90     MAC_VERSION_ENCODING = 148,
91     CURRENT_TRANSMIT_POWER = 147,
92     DOWNLINK_SERVICE_FLOW = 146,
93     UPLINK_SERVICE_FLOW = 145,
94     VENDOR_ID_EMCODING = 144,
95     VENDOR_SPECIFIC_INFORMATION = 143
96   };
97   /**
98    * Constructor
99    *
100    * \param type type
101    * \param length the length
102    * \param value TLV value
103    */
104   Tlv (uint8_t type, uint64_t length, const TlvValue & value);
105   Tlv (void);
106   ~Tlv (void);
107   /**
108    * Register this type.
109    * \return the TypeId.
110    */
111   static TypeId GetTypeId (void);
112   virtual TypeId GetInstanceTypeId (void) const;
113   virtual void Print (std::ostream &os) const;
114   virtual uint32_t GetSerializedSize (void) const;
115   virtual void Serialize (Buffer::Iterator start) const;
116   virtual uint32_t Deserialize (Buffer::Iterator start);
117   /**
118    * Get size of length field
119    * \returns the size of length field
120    */
121   uint8_t GetSizeOfLen (void) const;
122   /**
123    * Get type value
124    * \returns the type
125    */
126   uint8_t GetType (void) const;
127   /**
128    * Get length value
129    * \returns the length
130    */
131   uint64_t GetLength (void) const;
132   /**
133    * Peek value
134    * \returns the TLV value
135    */
136   TlvValue* PeekValue (void);
137   /**
138    * Copy TLV
139    * \returns a pointer to a TLV copy
140    */
141   Tlv * Copy (void) const;
142   /**
143    * Copy TlvValue
144    * \returns the TLV value
145    */
146   TlvValue * CopyValue (void) const;
147   /**
148    * assignment operator
149    * \param o the TLV to assign
150    * \returns the TLV
151    */
152   Tlv &operator = (Tlv const& o);
153   /**
154    * type conversion operator
155    * \param tlv the TLV
156    */
157   Tlv (const Tlv & tlv);
158 
159 private:
160   uint8_t m_type; ///< type
161   uint64_t m_length; ///< length
162   TlvValue * m_value; ///< value
163 };
164 
165 // ==============================================================================
166 /**
167  * \ingroup wimax
168  * \brief U8TlvValue class
169  */
170 class U8TlvValue : public TlvValue
171 {
172 public:
173   /**
174    * Constructor
175    *
176    * \param value value to encode
177    */
178   U8TlvValue (uint8_t value);
179   U8TlvValue ();
180   ~U8TlvValue (void);
181   virtual uint32_t GetSerializedSize (void) const;
182   virtual void Serialize (Buffer::Iterator start) const;
183   virtual uint32_t Deserialize (Buffer::Iterator start,uint64_t valueLen);
184   /**
185    * Deserialize from a buffer
186    * \param start the iterator
187    * \returns the size of the item
188    */
189   uint32_t Deserialize (Buffer::Iterator start);
190   /**
191    * Get value
192    * \returns the value
193    */
194   uint8_t GetValue (void) const;
195   /**
196    * Copy
197    * \returns a U8 TLV value
198    */
199   U8TlvValue * Copy (void) const;
200 private:
201   uint8_t  m_value; ///< value
202 };
203 
204 // ==============================================================================
205 /**
206  * \ingroup wimax
207  * \brief U16TlvValue class
208  */
209 class U16TlvValue : public TlvValue
210 {
211 public:
212   /**
213    * Constructor
214    *
215    * \param value value to encode
216    */
217   U16TlvValue (uint16_t value);
218   U16TlvValue ();
219   ~U16TlvValue (void);
220   virtual uint32_t GetSerializedSize (void) const;
221   virtual void Serialize (Buffer::Iterator start) const;
222   virtual uint32_t Deserialize (Buffer::Iterator start,uint64_t valueLen);
223   /**
224    * Deserialize from a buffer
225    * \param start the iterator
226    * \returns the size
227    */
228   uint32_t Deserialize (Buffer::Iterator start);
229   /**
230    * Get value
231    * \returns the value
232    */
233   uint16_t GetValue (void) const;
234   /**
235    * Copy
236    * \returns the U16 TLV value
237    */
238   virtual U16TlvValue * Copy (void) const;
239 private:
240   uint16_t  m_value; ///< value
241 };
242 
243 // ==============================================================================
244 /**
245  * \ingroup wimax
246  * \brief U32TlvValue class
247  */
248 class U32TlvValue : public TlvValue
249 {
250 public:
251   /**
252    * Constructor
253    *
254    * \param value to encode
255    */
256   U32TlvValue (uint32_t value);
257   U32TlvValue ();
258   ~U32TlvValue (void);
259 
260   virtual uint32_t GetSerializedSize (void) const;
261   virtual void Serialize (Buffer::Iterator start) const;
262   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLen);
263   /**
264    * Deserialize from a buffer
265    * \param start the iterator
266    * \returns the size
267    */
268   uint32_t Deserialize (Buffer::Iterator start);
269   /**
270    * Get value
271    * \returns the value
272    */
273   uint32_t GetValue (void) const;
274   /**
275    * Copy
276    * \returns the U32 TLV Value
277    */
278   virtual U32TlvValue * Copy (void) const;
279 private:
280   uint32_t  m_value; ///< value
281 };
282 
283 // ==============================================================================
284 
285 /**
286  * \ingroup wimax
287  * \brief this class is used to implement a vector of values in one tlv value field
288  */
289 class VectorTlvValue : public TlvValue
290 {
291 public:
292   /// TLV vector iterator typedef
293   typedef std::vector<Tlv*>::const_iterator Iterator;
294   VectorTlvValue (void);
295   ~VectorTlvValue (void);
296   virtual uint32_t GetSerializedSize (void) const;
297   virtual void Serialize (Buffer::Iterator start) const;
298   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLength) = 0;
299   /**
300    * Begin iterator
301    * \returns the beginning element
302    */
303   Iterator Begin () const;
304   /**
305    * End iterator
306    * \returns the ending element
307    */
308   Iterator End () const;
309   /**
310    * Add a TLV
311    * \param val the TLV value
312    */
313   void Add (const Tlv & val);
314   /**
315    * Copy
316    * \returns the vector TLV value
317    */
318   virtual VectorTlvValue * Copy (void) const = 0;
319 private:
320   std::vector<Tlv*>  * m_tlvList; ///< tlv list
321 };
322 
323 // ==============================================================================
324 /**
325  * \ingroup wimax
326  * \brief SfVectorTlvValue class
327  */
328 class SfVectorTlvValue : public VectorTlvValue
329 {
330 
331 public:
332   /// Type enumeration
333   enum Type
334   {
335     SFID = 1,
336     CID = 2,
337     Service_Class_Name = 3,
338     reserved1 = 4,
339     QoS_Parameter_Set_Type = 5,
340     Traffic_Priority = 6,
341     Maximum_Sustained_Traffic_Rate = 7,
342     Maximum_Traffic_Burst = 8,
343     Minimum_Reserved_Traffic_Rate = 9,
344     Minimum_Tolerable_Traffic_Rate = 10,
345     Service_Flow_Scheduling_Type = 11,
346     Request_Transmission_Policy = 12,
347     Tolerated_Jitter = 13,
348     Maximum_Latency = 14,
349     Fixed_length_versus_Variable_length_SDU_Indicator = 15,
350     SDU_Size = 16,
351     Target_SAID = 17,
352     ARQ_Enable = 18,
353     ARQ_WINDOW_SIZE = 19,
354     ARQ_RETRY_TIMEOUT_Transmitter_Delay = 20,
355     ARQ_RETRY_TIMEOUT_Receiver_Delay = 21,
356     ARQ_BLOCK_LIFETIME = 22,
357     ARQ_SYNC_LOSS = 23,
358     ARQ_DELIVER_IN_ORDER = 24,
359     ARQ_PURGE_TIMEOUT = 25,
360     ARQ_BLOCK_SIZE = 26,
361     reserved2 = 27,
362     CS_Specification = 28,
363     IPV4_CS_Parameters = 100
364   };
365   SfVectorTlvValue ();
366   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLength);
367   virtual SfVectorTlvValue * Copy (void) const;
368 
369 };
370 // ==============================================================================
371 
372 /**
373  * \ingroup wimax
374  * \brief this class implements the convergence sub-layer descriptor as a tlv vector
375  */
376 class CsParamVectorTlvValue : public VectorTlvValue
377 {
378 public:
379   /// Type enumeration
380   enum Type
381   {
382     Classifier_DSC_Action = 1,
383     Packet_Classification_Rule = 3,
384   };
385   CsParamVectorTlvValue ();
386   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLength);
387   virtual CsParamVectorTlvValue * Copy (void) const;
388 private:
389 };
390 
391 // ==============================================================================
392 
393 /**
394  * \ingroup wimax
395  * \brief this class implements the classifier descriptor as a tlv vector
396  */
397 class ClassificationRuleVectorTlvValue : public VectorTlvValue
398 {
399 public:
400   /// ClassificationRuleTlvType enumeration
401   enum ClassificationRuleTlvType
402   {
403     Priority = 1,
404     ToS = 2,
405     Protocol = 3,
406     IP_src = 4,
407     IP_dst = 5,
408     Port_src = 6,
409     Port_dst = 7,
410     Index = 14,
411   };
412   ClassificationRuleVectorTlvValue ();
413   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLength);
414   virtual ClassificationRuleVectorTlvValue * Copy (void) const;
415 private:
416 };
417 
418 // ==============================================================================
419 /**
420  * \ingroup wimax
421  * \brief TosTlvValue class
422  */
423 class TosTlvValue : public TlvValue
424 {
425 public:
426   TosTlvValue ();
427   /**
428    * Constructor
429    *
430    * \param low low value
431    * \param high high value
432    * \param mask the mask
433    */
434   TosTlvValue (uint8_t low, uint8_t high, uint8_t mask);
435   ~TosTlvValue ();
436   virtual uint32_t GetSerializedSize (void) const;
437   virtual void Serialize (Buffer::Iterator start) const;
438   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLength);
439   /**
440    * Get low part
441    * \returns the low part
442    */
443   uint8_t GetLow (void) const;
444   /**
445    * Get high part
446    * \returns the high part
447    */
448   uint8_t GetHigh (void) const;
449   /**
450    * Get the mask
451    * \returns the mask
452    */
453   uint8_t GetMask (void) const;
454   /**
455    * Copy
456    * \returns the TOS TLV value
457    */
458   virtual TosTlvValue * Copy () const;
459 private:
460   uint8_t m_low; ///< low
461   uint8_t m_high; ///< high
462   uint8_t m_mask; ///< mask
463 };
464 
465 // ==============================================================================
466 /**
467  * \ingroup wimax
468  * \brief PortRangeTlvValue class
469  */
470 class PortRangeTlvValue : public TlvValue
471 {
472 public:
473   /// PortRange structure
474   struct PortRange
475   {
476     uint16_t PortLow; ///< low
477     uint16_t PortHigh; ///< high
478   };
479   /// PortRange vector iterator typedef
480   typedef std::vector<struct PortRange>::const_iterator Iterator;
481   PortRangeTlvValue ();
482   ~PortRangeTlvValue ();
483   virtual uint32_t GetSerializedSize (void) const;
484   virtual void Serialize (Buffer::Iterator start) const;
485   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLength);
486   /**
487    * Add a range
488    * \param portLow the low port of the range
489    * \param portHigh the high port of the range
490    */
491   void Add (uint16_t portLow, uint16_t portHigh);
492   /**
493    * Begin iterator
494    * \returns the beginning element
495    */
496   Iterator Begin () const;
497   /**
498    * End iterator
499    * \returns the ending element
500    */
501   Iterator End () const;
502   /**
503    * Copy
504    * \returns the port range tlv value
505    */
506   virtual PortRangeTlvValue * Copy (void) const;
507 private:
508   std::vector<struct PortRange> * m_portRange; ///< port range
509 };
510 
511 // ==============================================================================
512 /**
513  * \ingroup wimax
514  * \brief ProtocolTlvValue class
515  */
516 class ProtocolTlvValue : public TlvValue
517 {
518 public:
519   ProtocolTlvValue ();
520   ~ProtocolTlvValue ();
521   /// Iterator typedef
522   typedef std::vector<uint8_t>::const_iterator Iterator;
523   virtual uint32_t GetSerializedSize (void) const;
524   virtual void Serialize (Buffer::Iterator start) const;
525   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLength);
526   /**
527    * Add protocol number
528    * \param protocol the protocol number
529    */
530   void Add (uint8_t protocol);
531   /**
532    * Begin iterator
533    * \returns the beginning element
534    */
535   Iterator Begin () const;
536   /**
537    * End iterator
538    * \return the ending element
539    */
540   Iterator End () const;
541   /**
542    * Copy
543    * \returns the protocol tlv value
544    */
545   virtual ProtocolTlvValue * Copy (void) const;
546 private:
547   std::vector<uint8_t> * m_protocol; ///< protocol
548 };
549 
550 // ==============================================================================
551 
552 /**
553  * \ingroup wimax
554  * \brief Ipv4AddressTlvValue class
555  */
556 class Ipv4AddressTlvValue : public TlvValue
557 {
558 public:
559   ///ipv4Addr structure
560   struct ipv4Addr
561   {
562     Ipv4Address Address; ///< address
563     Ipv4Mask Mask; ///< mask
564   };
565   /// IPv4 address vector iterator typedef
566   typedef std::vector<struct ipv4Addr>::const_iterator Iterator;
567   Ipv4AddressTlvValue ();
568   ~Ipv4AddressTlvValue ();
569   virtual uint32_t GetSerializedSize (void) const;
570   virtual void Serialize (Buffer::Iterator start) const;
571   virtual uint32_t Deserialize (Buffer::Iterator start, uint64_t valueLength);
572   /**
573    * Add IPv4 address and mask
574    * \param address the IPv4 address
575    * \param Mask the IPv4 mask
576    */
577   void Add (Ipv4Address address, Ipv4Mask Mask);
578   /**
579    * Begin iterator
580    * \returns the beginning element
581    */
582   Iterator Begin () const;
583   /**
584    * End iterator
585    * \returns the ending element
586    */
587   Iterator End () const;
588   virtual Ipv4AddressTlvValue * Copy () const;
589 private:
590   std::vector<struct ipv4Addr> * m_ipv4Addr; ///< ipv4 addr
591 };
592 
593 }
594 
595 #endif /* WIMAX_TLV_H */
596