pkt.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. // Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef PKT_H
  7. #define PKT_H
  8. #include <asiolink/io_address.h>
  9. #include <util/buffer.h>
  10. #include <dhcp/option.h>
  11. #include <dhcp/hwaddr.h>
  12. #include <dhcp/classify.h>
  13. #include <boost/date_time/posix_time/posix_time.hpp>
  14. #include <boost/shared_ptr.hpp>
  15. #include <utility>
  16. namespace isc {
  17. namespace dhcp {
  18. /// @brief RAII object enabling copying options retrieved from the
  19. /// packet.
  20. ///
  21. /// This object enables copying retrieved options from a packet within
  22. /// a scope in which this object exists. When the object goes out of scope
  23. /// copying options is disabled. This is applicable in cases when the
  24. /// server is going to invoke a callout (hook library) where copying options
  25. /// must be enabled by default. When the callouts return copying options
  26. /// should be disabled. The use of RAII object eliminates the need for
  27. /// explicitly re-renabling options copying and is safer in case of
  28. /// exceptions thrown by callouts and a presence of multiple exit points.
  29. template<typename PktType>
  30. class ScopedEnableOptionsCopy {
  31. public:
  32. /// @brief Pointer to an encapsulated packet.
  33. typedef boost::shared_ptr<PktType> PktTypePtr;
  34. /// @brief Constructor.
  35. ///
  36. /// Enables options copying on a packet(s).
  37. ///
  38. /// @param pkt1 Pointer to first packet.
  39. /// @param pkt2 Optional pointer to the second packet.
  40. ScopedEnableOptionsCopy(const PktTypePtr& pkt1,
  41. const PktTypePtr& pkt2 = PktTypePtr())
  42. : pkts_(pkt1, pkt2) {
  43. if (pkt1) {
  44. pkt1->setCopyRetrievedOptions(true);
  45. }
  46. if (pkt2) {
  47. pkt2->setCopyRetrievedOptions(true);
  48. }
  49. }
  50. /// @brief Destructor.
  51. ///
  52. /// Disables options copying on a packets.
  53. ~ScopedEnableOptionsCopy() {
  54. if (pkts_.first) {
  55. pkts_.first->setCopyRetrievedOptions(false);
  56. }
  57. if (pkts_.second) {
  58. pkts_.second->setCopyRetrievedOptions(false);
  59. }
  60. }
  61. private:
  62. /// @brief Holds a pointers to the packets.
  63. std::pair<PktTypePtr, PktTypePtr> pkts_;
  64. };
  65. /// @brief Base class for classes representing DHCP messages.
  66. ///
  67. /// This is a base class that holds common information (e.g. source
  68. /// and destination ports) and operations (e.g. add, get, delete options)
  69. /// for derived classes representing both DHCPv4 and DHCPv6 messages.
  70. /// The @c Pkt4 and @c Pkt6 classes derive from it.
  71. ///
  72. /// @note This is abstract class. Please instantiate derived classes
  73. /// such as @c Pkt4 or @c Pkt6.
  74. class Pkt {
  75. protected:
  76. /// @brief Constructor.
  77. ///
  78. /// This constructor is typically used for transmitted messages as it
  79. /// creates an empty (no options) packet. The constructor is protected,
  80. /// so only derived classes can call it. Pkt class cannot be instantiated
  81. /// anyway, because it is an abstract class.
  82. ///
  83. /// @param transid transaction-id
  84. /// @param local_addr local IPv4 or IPv6 address
  85. /// @param remote_addr remote IPv4 or IPv6 address
  86. /// @param local_port local UDP (one day also TCP) port
  87. /// @param remote_port remote UDP (one day also TCP) port
  88. Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
  89. const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
  90. uint16_t remote_port);
  91. /// @brief Constructor.
  92. ///
  93. /// This constructor is typically used for received messages as it takes
  94. /// a buffer that's going to be parsed as one of arguments. The constructor
  95. /// is protected, so only derived classes can call it. Pkt class cannot be
  96. /// instantiated anyway, because it is an abstract class.
  97. ///
  98. /// @param buf pointer to a buffer that contains on-wire data
  99. /// @param len length of the pointer specified in buf
  100. /// @param local_addr local IPv4 or IPv6 address
  101. /// @param remote_addr remote IPv4 or IPv6 address
  102. /// @param local_port local UDP (one day also TCP) port
  103. /// @param remote_port remote UDP (one day also TCP) port
  104. Pkt(const uint8_t* buf, uint32_t len,
  105. const isc::asiolink::IOAddress& local_addr,
  106. const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
  107. uint16_t remote_port);
  108. public:
  109. /// @brief Prepares on-wire format of DHCP (either v4 or v6) packet.
  110. ///
  111. /// Prepares on-wire format of message and all its options.
  112. /// A caller must ensure that options are stored in options_ field
  113. /// prior to calling this method.
  114. ///
  115. /// Output buffer will be stored in buffer_out_.
  116. /// The buffer_out_ should be cleared before writting to the buffer
  117. /// in the derived classes.
  118. ///
  119. /// @note This is a pure virtual method and must be implemented in
  120. /// the derived classes. The @c Pkt4 and @c Pkt6 class have respective
  121. /// implementations of this method.
  122. ///
  123. /// @throw InvalidOperation if packing fails
  124. virtual void pack() = 0;
  125. /// @brief Parses on-wire form of DHCP (either v4 or v6) packet.
  126. ///
  127. /// Parses received packet, stored in on-wire format in data_.
  128. ///
  129. /// Will create a collection of option objects that will
  130. /// be stored in options_ container.
  131. ///
  132. /// @note This is a pure virtual method and must be implemented in
  133. /// the derived classes. The @c Pkt4 and @c Pkt6 class have respective
  134. /// implementations of this method.
  135. ///
  136. /// Method will throw exception if packet parsing fails.
  137. ///
  138. /// @throw tbd
  139. virtual void unpack() = 0;
  140. /// @brief Returns reference to output buffer.
  141. ///
  142. /// Returned buffer will contain reasonable data only for
  143. /// output (TX) packet and after pack() was called.
  144. ///
  145. /// RX packet or TX packet before pack() will return buffer with
  146. /// zero length. This buffer is returned as non-const, so hooks
  147. /// framework (and user's callouts) can modify them if needed
  148. ///
  149. /// @note This buffer is only valid till object that returned it exists.
  150. ///
  151. /// @return reference to output buffer
  152. isc::util::OutputBuffer& getBuffer() { return (buffer_out_); };
  153. /// @brief Adds an option to this packet.
  154. ///
  155. /// Derived classes may provide more specialized implementations.
  156. /// In particular @c Pkt4 provides one that checks if option is
  157. /// unique.
  158. ///
  159. /// @param opt option to be added.
  160. virtual void addOption(const OptionPtr& opt);
  161. /// @brief Attempts to delete first suboption of requested type.
  162. ///
  163. /// If there are several options of the same type present, only
  164. /// the first option will be deleted.
  165. ///
  166. /// @param type Type of option to be deleted.
  167. ///
  168. /// @return true if option was deleted, false if no such option existed
  169. bool delOption(uint16_t type);
  170. /// @brief Returns text representation primary packet identifiers
  171. ///
  172. /// This method is intended to be used to provide as a consistent way to
  173. /// identify packets within log statements. Derivations should supply
  174. /// there own implementation.
  175. ///
  176. /// @return string with text representation
  177. virtual std::string getLabel() const {
  178. isc_throw(NotImplemented, "Pkt::getLabel()");
  179. }
  180. /// @brief Returns text representation of the packet.
  181. ///
  182. /// This function is useful mainly for debugging.
  183. ///
  184. /// @note This is a pure virtual method and must be implemented in
  185. /// the derived classes. The @c Pkt4 and @c Pkt6 class have respective
  186. /// implementations of this method.
  187. ///
  188. /// @return string with text representation
  189. virtual std::string toText() const = 0;
  190. /// @brief Returns packet size in binary format.
  191. ///
  192. /// Returns size of the packet in on-wire format or size needed to store
  193. /// it in on-wire format.
  194. ///
  195. /// @note This is a pure virtual method and must be implemented in
  196. /// the derived classes. The @c Pkt4 and @c Pkt6 class have respective
  197. /// implementations of this method.
  198. ///
  199. /// @return packet size in bytes
  200. virtual size_t len() = 0;
  201. /// @brief Returns message type (e.g. 1 = SOLICIT).
  202. ///
  203. /// @note This is a pure virtual method and must be implemented in
  204. /// the derived classes. The @c Pkt4 and @c Pkt6 class have respective
  205. /// implementations of this method.
  206. ///
  207. /// @return message type
  208. virtual uint8_t getType() const = 0;
  209. /// @brief Sets message type (e.g. 1 = SOLICIT).
  210. ///
  211. /// @note This is a pure virtual method and must be implemented in
  212. /// the derived classes. The @c Pkt4 and @c Pkt6 class have respective
  213. /// implementations of this method.
  214. ///
  215. /// @param type message type to be set
  216. virtual void setType(uint8_t type) = 0;
  217. /// @brief Returns name of the DHCP message.
  218. ///
  219. /// For all unsupported messages the derived classes must return
  220. /// "UNKNOWN".
  221. ///
  222. /// @return Ponter to "const" string containing DHCP message name.
  223. /// The implementations in the derived classes should statically
  224. /// allocate returned strings and the caller must not release the
  225. /// returned pointer.
  226. virtual const char* getName() const = 0;
  227. /// @brief Sets transaction-id value.
  228. ///
  229. /// @param transid transaction-id to be set.
  230. void setTransid(uint32_t transid) { transid_ = transid; }
  231. /// @brief Returns value of transaction-id field.
  232. ///
  233. /// @return transaction-id
  234. uint32_t getTransid() const { return (transid_); };
  235. /// @brief Checks whether a client belongs to a given class.
  236. ///
  237. /// @param client_class name of the class
  238. /// @return true if belongs
  239. bool inClass(const isc::dhcp::ClientClass& client_class);
  240. /// @brief Adds packet to a specified class.
  241. ///
  242. /// A packet can be added to the same class repeatedly. Any additional
  243. /// attempts to add to a class the packet already belongs to, will be
  244. /// ignored silently.
  245. ///
  246. /// @note It is a matter of naming convention. Conceptually, the server
  247. /// processes a stream of packets, with some packets belonging to given
  248. /// classes. From that perspective, this method adds a packet to specifed
  249. /// class. Implementation wise, it looks the opposite - the class name
  250. /// is added to the packet. Perhaps the most appropriate name for this
  251. /// method would be associateWithClass()? But that seems overly long,
  252. /// so I decided to stick with addClass().
  253. ///
  254. /// @param client_class name of the class to be added
  255. void addClass(const isc::dhcp::ClientClass& client_class);
  256. /// @brief Returns the class set
  257. ///
  258. /// @note This should be used only to iterate over the class set.
  259. /// @return
  260. const ClientClasses& getClasses() const { return (classes_); }
  261. /// @brief Unparsed data (in received packets).
  262. ///
  263. /// @warning This public member is accessed by derived
  264. /// classes directly. One of such derived classes is
  265. /// @ref perfdhcp::PerfPkt6. The impact on derived clasess'
  266. /// behavior must be taken into consideration before making
  267. /// changes to this member such as access scope restriction or
  268. /// data format change etc.
  269. OptionBuffer data_;
  270. protected:
  271. /// @brief Returns the first option of specified type without copying.
  272. ///
  273. /// This method is internally used by the @ref Pkt class and derived
  274. /// classes to retrieve a pointer to the specified option. This
  275. /// method doesn't copy the option before returning it to the
  276. /// caller.
  277. ///
  278. /// @param type Option type.
  279. ///
  280. /// @return Pointer to the option of specified type or NULL pointer
  281. /// if such option is not present.
  282. OptionPtr getNonCopiedOption(const uint16_t type) const;
  283. public:
  284. /// @brief Returns the first option of specified type.
  285. ///
  286. /// Returns the first option of specified type. Note that in DHCPv6 several
  287. /// instances of the same option are allowed (and frequently used).
  288. /// Also see @ref Pkt6::getOptions().
  289. ///
  290. /// The options will be only returned after unpack() is called.
  291. ///
  292. /// @param type option type we are looking for
  293. ///
  294. /// @return pointer to found option (or NULL)
  295. OptionPtr getOption(const uint16_t type);
  296. OptionPtr getOption(const uint16_t type) const {
  297. return (getNonCopiedOption(type));
  298. }
  299. /// @brief Controls whether the option retrieved by the @ref Pkt::getOption
  300. /// should be copied before being returned.
  301. ///
  302. /// Setting this value to true enables the mechanism of copying options
  303. /// retrieved from the packet to prevent accidental modifications of
  304. /// options that shouldn't be modified. The typical use case for this
  305. /// mechanism is to prevent hook library from modifying instance of
  306. /// an option within the packet that would also affect the value for
  307. /// this option within the Kea configuration structures.
  308. ///
  309. /// Kea doesn't copy option instances which it stores in the packet.
  310. /// It merely copy pointers into the packets. Thus, any modification
  311. /// to an option would change the value of this option in the
  312. /// Kea configuration. To prevent this, option copying should be
  313. /// enabled prior to passing the pointer to a packet to a hook library.
  314. ///
  315. /// Note that only only does this method causes the server to copy
  316. /// an option, but the copied option also replaces the original
  317. /// option within the packet. The option can be then freely modified
  318. /// and the modifications will only affect the instance of this
  319. /// option within the packet but not within the server configuration.
  320. ///
  321. /// @param copy Indicates if the options should be copied when
  322. /// retrieved (if true), or not copied (if false).
  323. void setCopyRetrievedOptions(const bool copy) {
  324. copy_retrieved_options_ = copy;
  325. }
  326. /// @brief Returns whether the copying of retrieved options is enabled.
  327. ///
  328. /// Also see @ref setCopyRetrievedOptions.
  329. ///
  330. /// @return true if retrieved options are copied.
  331. bool isCopyRetrievedOptions() const {
  332. return (copy_retrieved_options_);
  333. }
  334. /// @brief Update packet timestamp.
  335. ///
  336. /// Updates packet timestamp. This method is invoked
  337. /// by interface manager just before sending or
  338. /// just after receiving it.
  339. /// @throw isc::Unexpected if timestamp update failed
  340. void updateTimestamp();
  341. /// @brief Returns packet timestamp.
  342. ///
  343. /// Returns packet timestamp value updated when
  344. /// packet is received or send.
  345. ///
  346. /// @return packet timestamp.
  347. const boost::posix_time::ptime& getTimestamp() const {
  348. return timestamp_;
  349. }
  350. /// @brief Copies content of input buffer to output buffer.
  351. ///
  352. /// This is mostly a diagnostic function. It is being used for sending
  353. /// received packet. Received packet is stored in data_, but
  354. /// transmitted data is stored in buffer_out_. If we want to send packet
  355. /// that we just received, a copy between those two buffers is necessary.
  356. void repack();
  357. /// @brief Sets remote IP address.
  358. ///
  359. /// @param remote specifies remote address
  360. void setRemoteAddr(const isc::asiolink::IOAddress& remote) {
  361. remote_addr_ = remote;
  362. }
  363. /// @brief Returns remote IP address.
  364. ///
  365. /// @return remote address
  366. const isc::asiolink::IOAddress& getRemoteAddr() const {
  367. return (remote_addr_);
  368. }
  369. /// @brief Sets local IP address.
  370. ///
  371. /// @param local specifies local address
  372. void setLocalAddr(const isc::asiolink::IOAddress& local) {
  373. local_addr_ = local;
  374. }
  375. /// @brief Returns local IP address.
  376. ///
  377. /// @return local address
  378. const isc::asiolink::IOAddress& getLocalAddr() const {
  379. return (local_addr_);
  380. }
  381. /// @brief Sets local UDP (and soon TCP) port.
  382. ///
  383. /// This sets a local port, i.e. destination port for recently received
  384. /// packet or a source port for to be transmitted packet.
  385. ///
  386. /// @param local specifies local port
  387. void setLocalPort(uint16_t local) {
  388. local_port_ = local;
  389. }
  390. /// @brief Returns local UDP (and soon TCP) port.
  391. ///
  392. /// This sets a local port, i.e. destination port for recently received
  393. /// packet or a source port for to be transmitted packet.
  394. ///
  395. /// @return local port
  396. uint16_t getLocalPort() const {
  397. return (local_port_);
  398. }
  399. /// @brief Sets remote UDP (and soon TCP) port.
  400. ///
  401. /// This sets a remote port, i.e. source port for recently received
  402. /// packet or a destination port for to be transmitted packet.
  403. ///
  404. /// @param remote specifies remote port
  405. void setRemotePort(uint16_t remote) {
  406. remote_port_ = remote;
  407. }
  408. /// @brief Returns remote port.
  409. ///
  410. /// @return remote port
  411. uint16_t getRemotePort() const {
  412. return (remote_port_);
  413. }
  414. /// @brief Sets interface index.
  415. ///
  416. /// @param ifindex specifies interface index.
  417. void setIndex(uint32_t ifindex) {
  418. ifindex_ = ifindex;
  419. };
  420. /// @brief Returns interface index.
  421. ///
  422. /// @return interface index
  423. uint32_t getIndex() const {
  424. return (ifindex_);
  425. };
  426. /// @brief Returns interface name.
  427. ///
  428. /// Returns interface name over which packet was received or is
  429. /// going to be transmitted.
  430. ///
  431. /// @return interface name
  432. std::string getIface() const { return (iface_); };
  433. /// @brief Sets interface name.
  434. ///
  435. /// Sets interface name over which packet was received or is
  436. /// going to be transmitted.
  437. ///
  438. /// @return interface name
  439. void setIface(const std::string& iface ) { iface_ = iface; };
  440. /// @brief Sets remote hardware address.
  441. ///
  442. /// Sets hardware address (MAC) from an existing HWAddr structure.
  443. /// The remote address is a destination address for outgoing
  444. /// packet and source address for incoming packet. When this
  445. /// is an outgoing packet, this address will be used to
  446. /// construct the link layer header.
  447. ///
  448. /// @param hw_addr structure representing HW address.
  449. ///
  450. /// @throw BadValue if addr is null
  451. void setRemoteHWAddr(const HWAddrPtr& hw_addr);
  452. /// @brief Sets remote hardware address.
  453. ///
  454. /// Sets the destination hardware (MAC) address for the outgoing packet
  455. /// or source HW address for the incoming packet. When this
  456. /// is an outgoing packet this address will be used to construct
  457. /// the link layer header.
  458. ///
  459. /// @note mac_addr must be a buffer of at least hlen bytes.
  460. ///
  461. /// In a typical case, hlen field would be redundant, as it could
  462. /// be extracted from mac_addr.size(). However, the difference is
  463. /// when running on exotic hardware, like Infiniband, that had
  464. /// MAC addresses 20 bytes long. In that case, hlen is set to zero
  465. /// in DHCPv4.
  466. ///
  467. /// @param htype hardware type (will be sent in htype field)
  468. /// @param hlen hardware length (will be sent in hlen field)
  469. /// @param hw_addr pointer to hardware address
  470. void setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
  471. const std::vector<uint8_t>& hw_addr);
  472. /// @brief Returns the remote HW address obtained from raw sockets.
  473. ///
  474. /// @return remote HW address.
  475. HWAddrPtr getRemoteHWAddr() const {
  476. return (remote_hwaddr_);
  477. }
  478. /// @brief Returns MAC address.
  479. ///
  480. /// The difference between this method and getRemoteHWAddr() is that
  481. /// getRemoteHWAddr() returns only what was obtained from raw sockets.
  482. /// This method is more generic and can attempt to obtain MAC from
  483. /// varied sources: raw sockets, client-id, link-local IPv6 address,
  484. /// and various relay options.
  485. ///
  486. /// @note Technically the proper term for this information is a link layer
  487. /// address, but it is frequently referred to MAC or hardware address.
  488. /// Since we're calling the feature "MAC addresses in DHCPv6", we decided
  489. /// to keep the name of getMAC().
  490. ///
  491. /// hw_addr_src takes a combination of bit values specified in
  492. /// HWADDR_SOURCE_* constants.
  493. ///
  494. /// @param hw_addr_src a bitmask that specifies hardware address source
  495. HWAddrPtr getMAC(uint32_t hw_addr_src);
  496. /// @brief Virtual desctructor.
  497. ///
  498. /// There is nothing to clean up here, but since there are virtual methods,
  499. /// we define virtual destructor to ensure that derived classes will have
  500. /// a virtual one, too.
  501. virtual ~Pkt() {
  502. }
  503. /// @brief Classes this packet belongs to.
  504. ///
  505. /// This field is public, so the code outside of Pkt4 or Pkt6 class can
  506. /// iterate over existing classes. Having it public also solves the problem
  507. /// of returned reference lifetime. It is preferred to use @ref inClass and
  508. /// @ref addClass should be used to operate on this field.
  509. ClientClasses classes_;
  510. /// @brief Collection of options present in this message.
  511. ///
  512. /// @warning This public member is accessed by derived
  513. /// classes directly. One of such derived classes is
  514. /// @ref perfdhcp::PerfPkt6. The impact on derived clasess'
  515. /// behavior must be taken into consideration before making
  516. /// changes to this member such as access scope restriction or
  517. /// data format change etc.
  518. isc::dhcp::OptionCollection options_;
  519. protected:
  520. /// @brief Attempts to obtain MAC address from source link-local
  521. /// IPv6 address
  522. ///
  523. /// This method is called from getMAC(HWADDR_SOURCE_IPV6_LINK_LOCAL)
  524. /// and should not be called directly. It is not 100% reliable.
  525. /// The source IPv6 address does not necessarily have to be link-local
  526. /// (may be global or ULA) and even if it's link-local, it doesn't
  527. /// necessarily be based on EUI-64. For example, Windows supports
  528. /// RFC4941, which randomized IID part of the link-local address.
  529. /// If this method fails, it will return NULL.
  530. ///
  531. /// For direct message, it attempts to use remote_addr_ field. For relayed
  532. /// message, it uses peer-addr of the first relay.
  533. ///
  534. /// @note This is a pure virtual method and must be implemented in
  535. /// the derived classes. The @c Pkt6 class have respective implementation.
  536. /// This method is not applicable to DHCPv4.
  537. ///
  538. /// @return hardware address (or NULL)
  539. virtual HWAddrPtr getMACFromSrcLinkLocalAddr() = 0;
  540. /// @brief Attempts to obtain MAC address from relay option
  541. /// client-linklayer-addr
  542. ///
  543. /// This method is called from getMAC(HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION)
  544. /// and should not be called directly. It will extract the client's
  545. /// MAC/Hardware address from option client_linklayer_addr (RFC6939)
  546. /// inserted by the relay agent closest to the client.
  547. /// If this method fails, it will return NULL.
  548. ///
  549. /// @note This is a pure virtual method and must be implemented in
  550. /// the derived classes. The @c Pkt6 class have respective implementation.
  551. /// This method is not applicable to DHCPv4.
  552. ///
  553. /// @return hardware address (or NULL)
  554. virtual HWAddrPtr getMACFromIPv6RelayOpt() = 0;
  555. /// @brief Attempts to obtain MAC address from DUID-LL or DUID-LLT.
  556. ///
  557. /// This method is called from getMAC(HWADDR_SOURCE_DUID) and should not be
  558. /// called directly. It will attempt to extract MAC address information
  559. /// from DUID if its type is LLT or LL. If this method fails, it will
  560. /// return NULL.
  561. ///
  562. /// @note This is a pure virtual method and must be implemented in
  563. /// the derived classes. The @c Pkt6 class have respective implementation.
  564. /// This method is not applicable to DHCPv4.
  565. ///
  566. /// @return hardware address (or NULL)
  567. virtual HWAddrPtr getMACFromDUID() = 0;
  568. /// @brief Attempts to obtain MAC address from remote-id relay option.
  569. ///
  570. /// This method is called from getMAC(HWADDR_SOURCE_REMOTE_ID) and should not be
  571. /// called directly. It will attempt to extract MAC address information
  572. /// from remote-id option inserted by a relay agent closest to the client.
  573. /// If this method fails, it will return NULL.
  574. ///
  575. /// @note This is a pure virtual method and must be implemented in
  576. /// the derived classes. The @c Pkt6 class have respective implementation.
  577. /// This method is not applicable to DHCPv4.
  578. ///
  579. /// @return hardware address (or NULL)
  580. virtual HWAddrPtr getMACFromRemoteIdRelayOption() = 0;
  581. /// @brief Attempts to convert IPv6 address into MAC.
  582. ///
  583. /// Utility method that attempts to convert link-local IPv6 address to the
  584. /// MAC address. That works only for link-local IPv6 addresses that are
  585. /// based on EUI-64.
  586. ///
  587. /// @note This method uses hardware type of the interface the packet was
  588. /// received on. If you have multiple access technologies in your network
  589. /// (e.g. client connected to WiFi that relayed the traffic to the server
  590. /// over Ethernet), hardware type may be invalid.
  591. ///
  592. /// @param addr IPv6 address to be converted
  593. /// @return hardware address (or NULL)
  594. HWAddrPtr
  595. getMACFromIPv6(const isc::asiolink::IOAddress& addr);
  596. /// @brief Attempts to extract MAC/Hardware address from DOCSIS options
  597. /// inserted by the modem itself.
  598. ///
  599. /// This is a generic mechanism for extracting hardware address from the
  600. /// DOCSIS options.
  601. ///
  602. /// @note This is a pure virtual method and must be implemented in
  603. /// the derived classes. The @c Pkt6 class have respective implementation.
  604. /// This method is currently not implemented in DHCPv4.
  605. ///
  606. /// @return hardware address (if necessary DOCSIS suboptions are present)
  607. virtual HWAddrPtr getMACFromDocsisModem() = 0;
  608. /// @brief Attempts to extract MAC/Hardware address from DOCSIS options
  609. /// inserted by the CMTS (the relay agent)
  610. ///
  611. /// This is a generic mechanism for extracting hardware address from the
  612. /// DOCSIS options.
  613. ///
  614. /// @note This is a pure virtual method and must be implemented in
  615. /// the derived classes. The @c Pkt6 class have respective implementation.
  616. /// This method is currently not implemented in DHCPv4.
  617. ///
  618. /// @return hardware address (if necessary DOCSIS suboptions are present)
  619. virtual HWAddrPtr getMACFromDocsisCMTS() = 0;
  620. /// Transaction-id (32 bits for v4, 24 bits for v6)
  621. uint32_t transid_;
  622. /// Name of the network interface the packet was received/to be sent over.
  623. std::string iface_;
  624. /// @brief Interface index.
  625. ///
  626. /// Each network interface has assigned an unique ifindex.
  627. /// It is a functional equivalent of a name, but sometimes more useful, e.g.
  628. /// when using odd systems that allow spaces in interface names.
  629. int ifindex_;
  630. /// @brief Local IP (v4 or v6) address.
  631. ///
  632. /// Specifies local IPv4 or IPv6 address. It is a destination address for
  633. /// received packet, and a source address if it packet is being transmitted.
  634. isc::asiolink::IOAddress local_addr_;
  635. /// @brief Remote IP address.
  636. ///
  637. /// Specifies local IPv4 or IPv6 address. It is source address for received
  638. /// packet and a destination address for packet being transmitted.
  639. isc::asiolink::IOAddress remote_addr_;
  640. /// local TDP or UDP port
  641. uint16_t local_port_;
  642. /// remote TCP or UDP port
  643. uint16_t remote_port_;
  644. /// Output buffer (used during message transmission)
  645. ///
  646. /// @warning This protected member is accessed by derived
  647. /// classes directly. One of such derived classes is
  648. /// @ref perfdhcp::PerfPkt6. The impact on derived clasess'
  649. /// behavior must be taken into consideration before making
  650. /// changes to this member such as access scope restriction or
  651. /// data format change etc.
  652. isc::util::OutputBuffer buffer_out_;
  653. /// @brief Indicates if a copy of the retrieved option should be
  654. /// returned when @ref Pkt::getOption is called.
  655. ///
  656. /// @see the documentation for @ref Pkt::setCopyRetrievedOptions.
  657. bool copy_retrieved_options_;
  658. /// packet timestamp
  659. boost::posix_time::ptime timestamp_;
  660. // remote HW address (src if receiving packet, dst if sending packet)
  661. HWAddrPtr remote_hwaddr_;
  662. private:
  663. /// @brief Generic method that validates and sets HW address.
  664. ///
  665. /// This is a generic method used by all modifiers of this class
  666. /// which set class members representing HW address.
  667. ///
  668. /// @param htype hardware type.
  669. /// @param hlen hardware length.
  670. /// @param hw_addr pointer to actual hardware address.
  671. /// @param [out] storage pointer to a class member to be modified.
  672. ///
  673. /// @trow isc::OutOfRange if invalid HW address specified.
  674. virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
  675. const std::vector<uint8_t>& hw_addr,
  676. HWAddrPtr& storage);
  677. };
  678. }; // namespace isc::dhcp
  679. }; // namespace isc
  680. #endif