libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
ipv6.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_IPV6_h
31 #define TINS_IPV6_h
32 
33 #include "macros.h"
34 #include "cxxstd.h"
35 #include "pdu.h"
36 #include "endianness.h"
37 #include "small_uint.h"
38 #include "pdu_option.h"
39 #include "ipv6_address.h"
40 
41 namespace Tins {
42 namespace Memory {
43 
44 class OutputMemoryStream;
45 
46 } // Memory
47 
48 class PacketSender;
49 
54 class TINS_API IPv6 : public PDU {
55 public:
59  static const PDU::PDUType pdu_flag = PDU::IPv6;
60 
65 
70 
74  typedef std::vector<ext_header> headers_type;
75 
80  HOP_BY_HOP = 0,
81  DESTINATION_ROUTING_OPTIONS = 60,
82  ROUTING = 43,
83  FRAGMENT = 44,
84  AUTHENTICATION = 51,
85  SECURITY_ENCAPSULATION = 50,
86  DESTINATION_OPTIONS = 60,
87  MOBILITY = 135,
88  NO_NEXT_HEADER = 59
89  };
90 
94  enum OptionType {
95  PAD_1 = 0,
96  PAD_N = 1,
97  JUMBO_PAYLOAD = 0xC2,
98  };
99 
106  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
107 
116  IPv6(address_type ip_dst = address_type(),
117  address_type ip_src = address_type(),
118  PDU* child = 0);
119 
130  IPv6(const uint8_t* buffer, uint32_t total_sz);
131 
132  // Getters
133 
139  return header_.version;
140  }
141 
146  uint8_t traffic_class() const {
147  #if TINS_IS_LITTLE_ENDIAN
148  return ((header_.traffic_class << 4) & 0xf0) |
149  ((header_.flow_label[0] >> 4) & 0x0f);
150  #else
151  return header_.traffic_class;
152  #endif
153  }
154 
160  #if TINS_IS_LITTLE_ENDIAN
161  return ((header_.flow_label[0] & 0x0f) << 16)
162  | (header_.flow_label[1] << 8)
163  | (header_.flow_label[2]);
164  #else
165  return header_.flow_label;
166  #endif
167  }
168 
173  uint16_t payload_length() const {
174  return Endian::be_to_host(header_.payload_length);
175  }
176 
181  uint8_t next_header() const {
182  return header_.next_header;
183  }
184 
189  uint8_t hop_limit() const {
190  return header_.hop_limit;
191  }
192 
198  return header_.src_addr;
199  }
200 
206  return header_.dst_addr;
207  }
208 
213  const headers_type& headers() const {
214  return ext_headers_;
215  }
216 
217  // Setters
218 
223  void version(small_uint<4> new_version);
224 
229  void traffic_class(uint8_t new_traffic_class);
230 
235  void flow_label(small_uint<20> new_flow_label);
236 
241  void payload_length(uint16_t new_payload_length);
242 
247  void next_header(uint8_t new_next_header);
248 
253  void hop_limit(uint8_t new_hop_limit);
254 
259  void src_addr(const address_type& new_src_addr);
260 
265  void dst_addr(const address_type& new_dst_addr);
266 
272  uint32_t header_size() const;
273 
281  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
282 
286  IPv6* clone() const {
287  return new IPv6(*this);
288  }
289 
294  PDUType pdu_type() const { return pdu_flag; }
295 
296  #ifndef BSD
297 
300  void send(PacketSender& sender, const NetworkInterface &);
301  #endif
302 
309  TINS_DEPRECATED(void add_ext_header(const ext_header& header));
310 
317  void add_header(const ext_header& header);
318 
319  #if TINS_IS_CXX11
320 
326  void add_header(ext_header&& header) {
327  ext_headers_.emplace_back(std::move(header));
328  }
329 
335  template <typename... Args>
336  void add_header(Args&&... args) {
337  ext_headers_.emplace_back(std::forward<Args>(args)...);
338  }
339 
340  #endif // TINS_IS_CXX11
341 
352  const ext_header* search_header(ExtensionHeader id) const;
353 private:
354  void write_serialization(uint8_t* buffer, uint32_t total_sz);
355  void set_last_next_header(uint8_t value);
356  uint32_t calculate_headers_size() const;
357  static void write_header(const ext_header& header, Memory::OutputMemoryStream& stream);
358  static bool is_extension_header(uint8_t header_id);
359 
360  TINS_BEGIN_PACK
361  struct ipv6_header {
362  #if TINS_IS_BIG_ENDIAN
363  uint32_t version:4,
364  traffic_class:8,
365  flow_label:20;
366  uint32_t payload_length:16,
367  next_header:8,
368  hop_limit:8;
369  #else
370  uint8_t traffic_class:4,
371  version:4;
372  uint8_t flow_label[3];
373  uint16_t payload_length;
374  uint8_t next_header;
375  uint8_t hop_limit;
376  #endif
377  uint8_t src_addr[16], dst_addr[16];
378  } TINS_END_PACK;
379 
380  ipv6_header header_;
381  headers_type ext_headers_;
382  uint8_t next_header_;
383 };
384 }
385 
386 #endif // TINS_IPV6_h
OptionType
Definition: ipv6.h:94
small_uint< 20 > flow_label() const
Getter for the flow_label field.
Definition: ipv6.h:159
void add_header(ext_header &&header)
Definition: ipv6.h:326
std::vector< ext_header > headers_type
Definition: ipv6.h:74
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
uint8_t hop_limit() const
Getter for the hop_limit field.
Definition: ipv6.h:189
address_type src_addr() const
Getter for the src_addr field.
Definition: ipv6.h:197
uint16_t payload_length() const
Getter for the payload_length field.
Definition: ipv6.h:173
uint8_t traffic_class() const
Getter for the traffic_class field.
Definition: ipv6.h:146
IPv6Address address_type
Definition: ipv6.h:64
Sends packets through a network interface.
Definition: packet_sender.h:116
Represents a PDU option field.
Definition: pdu_option.h:201
PDUOption< uint8_t, IPv6 > ext_header
Definition: ipv6.h:69
void add_header(Args &&...args)
Definition: ipv6.h:336
address_type dst_addr() const
Getter for the dst_addr field.
Definition: ipv6.h:205
small_uint< 4 > version() const
Getter for the version field.
Definition: ipv6.h:138
Definition: ipv6.h:54
const headers_type & headers() const
Getter for the IPv6 extension headers.
Definition: ipv6.h:213
ExtensionHeader
Definition: ipv6.h:79
PDUType pdu_type() const
Getter for the PDU's type.
Definition: ipv6.h:294
Abstraction of a network interface.
Definition: network_interface.h:47
uint8_t next_header() const
Getter for the next_header field.
Definition: ipv6.h:181
IPv6 * clone() const
Definition: ipv6.h:286
Definition: ipv6_address.h:44
Base class for protocol data units.
Definition: pdu.h:107