Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
fipv6frame.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef FIPV6FRAME_H
6 #define FIPV6FRAME_H 1
7 
8 #include <map>
9 #include <inttypes.h>
10 #include <endian.h>
11 #ifndef htobe16
12  #include "../endian_conversion.h"
13 #endif
14 
15 #include <iostream>
16 
17 #include "../fframe.h"
18 #include "../caddress.h"
19 #include "rofl/datapath/pipeline/common/large_types.h"
20 
21 
22 namespace rofl
23 {
24 
25 // error classes
26 class eIPv6FrameBase : public eFrameBase {}; // base error class for cpppoepacket
27 class eIPv6FrameTagNotFound : public eIPv6FrameBase {}; // pppoe tag not found
28 class eIPv6FrameInvalidSyntax : public eIPv6FrameBase {}; // frame has invalid syntax
29 class eIPv6FrameInval : public eIPv6FrameBase {}; // invalid parameter
30 class eIPv6FrameNotFound : public eIPv6FrameBase {}; // element not found
31 
32 
33 
34 class fipv6ext : public fframe {
35 private:
36 
37  std::string info;
38 
39 public: // static
40 
41  // IPv6 generic extension header
42  struct ipv6_ext_hdr_t {
43  uint8_t nxthdr;
44  uint8_t len;
45  uint8_t data[0];
46  } __attribute__((packed));
47 
48 public:
49 
50  struct ipv6_ext_hdr_t *exthdr;
51 
52 public:
53  // default constructor
54  fipv6ext() :
55  fframe((size_t)0),
56  exthdr(0)
57  {};
58  // default constructor
59  fipv6ext(struct ipv6_ext_hdr_t* hdr, size_t hdrlen) throw (eIPv6FrameInval) :
60  fframe((uint8_t*)hdr, hdrlen),
61  exthdr((struct ipv6_ext_hdr_t*)soframe())
62  {
63  if (hdrlen < 8) {
64  throw eIPv6FrameInval();
65  }
66  };
67  // default constructor
68  fipv6ext(uint8_t* hdr, size_t hdrlen) throw (eIPv6FrameInval) :
69  fframe(hdr, hdrlen),
70  exthdr((struct ipv6_ext_hdr_t*)soframe())
71  {
72  if (hdrlen < 8) {
73  throw eIPv6FrameInval();
74  }
75  };
76  // virtual destructor
77  virtual
78  ~fipv6ext() {
79  };
80  // copy constructor
81  fipv6ext(fipv6ext const& ipv6ext) : fframe(ipv6ext.framelen()) {
82  *this = ipv6ext;
83  };
84  // assignment operator
85  fipv6ext& operator= (fipv6ext const& ipv6ext) {
86  if (this == &ipv6ext)
87  return *this;
88  fframe::operator= (ipv6ext);
89  exthdr = (struct ipv6_ext_hdr_t*)soframe();
90  return *this;
91  };
92  const char*
93  c_str() {
94 #if 0
95  cvastring vas(256);
96  info.assign(vas("[IPv6-ext-hdr(%p): nxthdr:%d hdrextlen:%d block-cnt:%d bytes-len:%d]",
97  exthdr, exthdr->nxthdr, exthdr->len, (exthdr->len + 1), (exthdr->len + 1) * 8));
98 #endif
99  return info.c_str();
100  };
101 };
102 
103 
104 
105 
106 
107 
111 class fipv6frame : public fframe {
112 
113 private: // data structures
114 
115  std::string info; //< info string
116 
117 public: // static
118 
119  #define IPV6_ADDR_LEN 16
120  #define IPV6_VERSION 6
121 
122  /* ipv6 constants and definitions */
123  // ipv6 ethernet types
124  enum ipv6_ether_t {
125  IPV6_ETHER = 0x86dd,
126  };
127 
128  // IPv6 header
129  struct ipv6_hdr_t {
130  uint8_t bytes[4]; // version + tc + flow-label
131  uint16_t payloadlen;
132  uint8_t nxthdr;
133  uint8_t hoplimit;
134  uint8_t src[IPV6_ADDR_LEN];
135  uint8_t dst[IPV6_ADDR_LEN];
136  uint8_t data[0];
137  } __attribute__((packed));
138 
139  enum ipv6_ext_t {
140  IPPROTO_IPV6_HOPOPT = 0,
141  IPPROTO_ICMP = 1,
142  IPPROTO_TCP = 6,
143  IPPROTO_UDP = 17,
144  IPV6_IP_PROTO = 41,
145  IPPROTO_IPV6_ROUTE = 43,
146  IPPROTO_IPV6_FRAG = 44,
147  IPPROTO_IPV6_ICMP = 58,
148  IPPROTO_IPV6_NONXT = 59,
149  IPPROTO_IPV6_OPTS = 60,
150  IPPROTO_IPV6_MIPV6 = 135,
151  };
152  /* ipv6 definitions */
153 
154 public: // data structures
155 
156  struct ipv6_hdr_t *ipv6_hdr; // pointer to pppoe header
157  uint8_t *ipv6data; // payload data
158  size_t ipv6datalen; // ppp data length
159  std::map<enum ipv6_ext_t, fipv6ext> ipv6exts; // IPv6 extensions headers
160 
161 public: // methods
162 
163 
167  fipv6frame(
168  uint8_t* data,
169  size_t datalen);
170 
171 
175  virtual
176  ~fipv6frame();
177 
178 
182  void
184 
185 
189  fipv6ext&
190  get_ext_hdr(enum ipv6_ext_t type)
191  throw (eIPv6FrameNotFound);
192 
193 
194 public: // overloaded from fframe
195 
198  virtual bool
199  complete() const;
200 
203  virtual size_t
204  need_bytes() const;
205 
209  virtual void
210  validate(uint16_t total_len = 0) const;
211 
215  virtual void
216  initialize() throw (eIPv6FrameInval);
217 
221  virtual void
223  uint8_t *data, size_t datalen) throw (eFrameOutOfRange);
224 
228  virtual uint8_t*
229  payload() const throw (eFrameNoPayload);
230 
234  virtual size_t
235  payloadlen() const throw (eFrameNoPayload);
236 
237 public:
238 
241  void
242  set_version(uint8_t version = 0x06);
243 
246  uint8_t
247  get_version();
248 
251  void
252  set_traffic_class(uint8_t tc);
253 
256  void
257  set_dscp(uint8_t dscp);
258 
261  void
262  set_ecn(uint8_t ecn);
263 
266  uint8_t
267  get_traffic_class() const;
268 
271  uint8_t
272  get_dscp() const;
273 
276  uint8_t
277  get_ecn() const;
278 
281  void
282  set_flow_label(uint32_t flabel);
283 
286  uint32_t
287  get_flow_label() const;
288 
291  void
292  set_payload_length(uint16_t len);
293 
296  uint16_t
297  get_payload_length() const;
298 
301  void
302  set_next_header(uint8_t nxthdr);
303 
306  uint8_t
307  get_next_header();
308 
311  void
312  set_hop_limit(uint8_t hops);
313 
316  uint8_t
317  get_hop_limit();
318 
321  void
322  dec_hop_limit();
323 
326  void
327  set_ipv6_src(uint8_t *somem, size_t memlen) throw (eIPv6FrameInval);
328 
331  void
332  set_ipv6_src(cmemory const& src) throw (eIPv6FrameInval);
333 
336  void
337  set_ipv6_src(caddress_in6 const& src);
338 
342  get_ipv6_src() const;
343 
346  void
347  set_ipv6_dst(uint8_t *somen, size_t memlen) throw (eIPv6FrameInval);
348 
351  void
352  set_ipv6_dst(cmemory const& dst) throw (eIPv6FrameInval);
353 
356  void
357  set_ipv6_dst(caddress_in6 const& dst);
358 
362  get_ipv6_dst() const;
363 
364 
365 
366 private: // methods
367 
368 
369 
370 public:
371 
372  friend std::ostream&
373  operator<< (std::ostream& os, fipv6frame const& ipv6) {
374  os << dynamic_cast<fframe const&>( ipv6 );
375  os << indent(2) << "<fipv6frame: ";
376  os << "dst:" << ipv6.get_ipv6_dst() << " ";
377  os << "src:" << ipv6.get_ipv6_src() << " ";
378  os << "tc:" << (unsigned int)ipv6.get_traffic_class() << " ";
379  os << "flowlabel:" << (unsigned int)ipv6.get_flow_label() << " ";
380  os << ">" << std::endl;
381  return os;
382  };
383 };
384 
385 }; // end of namespace
386 
387 #endif
virtual void payload_insert(uint8_t *data, size_t datalen)
Definition: fipv6frame.cc:138
fipv6frame(uint8_t *data, size_t datalen)
Definition: fipv6frame.cc:9
Definition: caddress.h:589
Definition: fipv6frame.h:27
Definition: fframe.h:21
Definition: fipv6frame.h:29
fframe & operator=(const fframe &frame)
Definition: fframe.cc:79
virtual uint8_t * payload() const
Definition: fipv6frame.cc:147
void ipv6_calc_checksum()
Definition: fipv6frame.cc:177
virtual void initialize()
Definition: fipv6frame.cc:42
Definition: fframe.h:19
Definition: fipv6frame.h:28
fframe(uint8_t *_data=NULL, size_t _datalen=0)
Definition: fframe.cc:9
Definition: fipv6frame.h:129
Definition: fipv6frame.h:34
virtual size_t framelen() const
Definition: fframe.h:202
Definition: fipv6frame.h:42
Definition: fipv6frame.h:26
virtual size_t need_bytes() const
Definition: fipv6frame.cc:120
virtual ~fipv6frame()
Definition: fipv6frame.cc:23
void set_ipv6_dst(uint8_t *somen, size_t memlen)
Definition: fipv6frame.cc:383
virtual void validate(uint16_t total_len=0) const
Definition: fipv6frame.cc:166
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
virtual uint8_t * soframe() const
Definition: fframe.h:192
Definition: fframe.h:20
Definition: fipv6frame.h:30
Definition: logging.h:76
void set_ipv6_src(uint8_t *somem, size_t memlen)
Definition: fipv6frame.cc:339
Definition: fipv6frame.h:111
virtual bool complete() const
Definition: fipv6frame.cc:106
virtual size_t payloadlen() const
Definition: fipv6frame.cc:156
Definition: fframe.h:31