callouts.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include <hooks/hooks.h>
  2. #include <dhcp/pkt4.h>
  3. #include <dhcp/hwaddr.h>
  4. #include <dhcpsrv/subnet.h>
  5. #include <dhcpsrv/lease.h>
  6. #include <dhcpsrv/host.h>
  7. #include <util/strutil.h>
  8. #include <dhcp/option_string.h>
  9. #include <dhcp/option4_addrlst.h>
  10. #include <dhcp/docsis3_option_defs.h>
  11. #include <string>
  12. #include <dhcp/dhcp6.h>
  13. #include <dhcp/pkt6.h>
  14. #include <dhcp/option6_ia.h>
  15. #include <boost/foreach.hpp>
  16. #include "logger.h"
  17. #include "common.h"
  18. using namespace isc::dhcp;
  19. using namespace isc::hooks;
  20. extern "C" {
  21. #include <numeric> //inner_product
  22. #include <functional> //plus, equal_to, not2
  23. #include <algorithm>
  24. #include <string>
  25. #include <stdexcept>
  26. #include "mysql_connection.h"
  27. MySQLConnection conn;
  28. std::string getMacFromPacket(Pkt4Ptr);
  29. /**
  30. * Get the cablemodem mac from (option 82) or if vendor_class is *docsis* get the c_hwaddr
  31. */
  32. std::string getMacFromPacket(Pkt4Ptr pkt)
  33. {
  34. std::string rtr = "";
  35. if (pkt->getOption(DHO_DHCP_AGENT_OPTIONS)) {
  36. rtr = pkt->getOption(DHO_DHCP_AGENT_OPTIONS)->toHexString(false).substr(6,12);
  37. std::vector<uint8_t> binary;
  38. isc::util::str::decodeFormattedHexString(rtr, binary);
  39. } else if (pkt->getOption(DHO_VENDOR_CLASS_IDENTIFIER)) {
  40. std::string docsis_vendor_class = pkt->getOption(DHO_VENDOR_CLASS_IDENTIFIER)->toString().substr(2,6);
  41. if (docsis_vendor_class.compare("docsis") == 0) {
  42. rtr = pkt->getMAC(HWAddr::HWADDR_SOURCE_RAW)->toText(false);
  43. isc::util::str::uppercase(rtr);
  44. rtr.erase(std::remove(rtr.begin(), rtr.end(), ':'), rtr.end());
  45. }
  46. }
  47. return rtr;
  48. }
  49. enum HostType{ CABLEMODEM, EMTA, CPE };
  50. std::string HostClass[] = { "cm", "mta", "cpe" };
  51. HostType getHostType(Pkt4Ptr pkt)
  52. {
  53. HostType rtr = CPE;
  54. std::string cm_mac = getMacFromPacket(pkt);
  55. //Non Empty MAC
  56. if (cm_mac.size() != 0) {
  57. std::string chwdr = pkt->getHWAddr()->toText(false);
  58. isc::util::str::uppercase(chwdr);
  59. chwdr.erase(std::remove(chwdr.begin(), chwdr.end(), ':'), chwdr.end());
  60. std::string opt82mac = "";
  61. if (pkt->getOption(DHO_DHCP_AGENT_OPTIONS)) {
  62. opt82mac = pkt->getOption(DHO_DHCP_AGENT_OPTIONS)->toHexString(false).substr(6,12);
  63. std::vector<uint8_t> binary;
  64. isc::util::str::decodeFormattedHexString(opt82mac, binary);
  65. }
  66. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("hwaddr: " + chwdr);
  67. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("remoteid: "+opt82mac);
  68. if (chwdr == opt82mac) {
  69. rtr = CABLEMODEM;
  70. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("option 82 mac == chwdr ==> CABLEMODEM");
  71. } else {
  72. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("dou't " + chwdr + " !== " + opt82mac);
  73. if (pkt->getOption(DHO_VENDOR_CLASS_IDENTIFIER)) {
  74. std::string docsis_vendor_class = pkt->getOption(DHO_VENDOR_CLASS_IDENTIFIER)->toString().substr(0,6);
  75. std::string pktc_vendor_class = pkt->getOption(DHO_VENDOR_CLASS_IDENTIFIER)->toString().substr(0,4);
  76. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("Checking Vendor Class ID");
  77. if (docsis_vendor_class.compare("docsis") == 0) {
  78. rtr = CABLEMODEM;
  79. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("docsis en vendor class ==> CABLEMODEM");
  80. } else {
  81. if (pktc_vendor_class.compare("pktc") == 0) {
  82. rtr = EMTA;
  83. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("pktc en vendor class ==> EMTA");
  84. } else {
  85. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("vendor class unknown");
  86. }
  87. }
  88. } else {
  89. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("no vendor class");
  90. }
  91. }
  92. } else {
  93. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("Empty MAC fomr getMacFromPacket ==> CPE " + std::to_string(cm_mac.size()));
  94. if (pkt->isRelayed()) {
  95. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("Pakcet Relayed");
  96. }
  97. }
  98. return rtr;
  99. }
  100. /* IPv4 callouts */
  101. int pkt4_receive(CalloutHandle& handle)
  102. {
  103. Pkt4Ptr query;
  104. handle.getArgument("query4", query);
  105. // Busco la class para la mac que viene en el paquete
  106. // en fd3_dhcp.host, si no existe el host cargado no tiene class
  107. std::string mac = getMacFromPacket(query);
  108. std::string MAC = mac;
  109. std::transform(mac.begin(), mac.end(), mac.begin(), ::tolower);
  110. std::string sql_query = "SELECT state FROM `host` WHERE `mac` LIKE '" + mac + "';";
  111. std::string class_map = conn.executeQuery((sql_query).c_str());
  112. if (class_map != "") {
  113. cm_map[MAC] = class_map;
  114. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("cablemodem " + mac + " class " + class_map);
  115. } else {
  116. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("cablemodem " + mac + " class not found ");
  117. }
  118. std::vector<std::string> list_class;
  119. HWAddrPtr remote_id;
  120. HostType ht = getHostType(query);
  121. list_class.push_back(HostClass[ht]);
  122. std::map<std::string,std::string>::iterator it = cm_map.find(MAC);
  123. if (it != cm_map.end()) {
  124. list_class.push_back(cm_map[MAC]);
  125. }
  126. std::string me, me2;
  127. BOOST_FOREACH(me, list_class) {
  128. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("class : " + me);
  129. BOOST_FOREACH(me2, list_class) {
  130. if (me.compare(me2) != 0) {
  131. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("class : " + me + "-" + me2);
  132. query->addClass(me + "-" + me2);
  133. }
  134. }
  135. query->addClass(me);
  136. Host::IdentifierType type;
  137. std::vector<uint8_t> id((me + "-" + mac).begin(), (me + "-" + mac).end());
  138. type = Host::IDENT_FLEX;
  139. handle.setArgument("id_value", id);
  140. handle.setArgument("id_type", type);
  141. }
  142. return 0;
  143. }
  144. int pkt4_send(CalloutHandle& handle)
  145. {
  146. Pkt4Ptr response;
  147. Pkt4Ptr query;
  148. handle.getArgument("response4", response);
  149. handle.getArgument("query4", query);
  150. uint8_t ufile_name[128] = "";
  151. std::string mac;
  152. HostType ht = getHostType(query);
  153. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("ht : " + std::to_string(ht));
  154. if (ht == CABLEMODEM or ht == EMTA) {
  155. mac = getMacFromPacket(query);
  156. }
  157. if (ht == CABLEMODEM) {
  158. std::string file = mac + ".bin";
  159. isc::util::str::lowercase(file);
  160. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("file : " + file);
  161. strcpy((char*)ufile_name, file.c_str());
  162. response->setFile(ufile_name, strlen((char *)ufile_name));
  163. std::map<std::string,std::string>::iterator it = option122_map.find(mac);
  164. if (it != option122_map.end()) {
  165. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("cablemodem " + mac + " found for MTA Activation on " + option122_map[mac]);
  166. OptionPtr option1(new Option(Option::V4, 122));
  167. Option4AddrLstPtr opt_server(new Option4AddrLst(1));
  168. opt_server->setAddress(isc::asiolink::IOAddress("255.255.255.255"));//option122_map[mac]));
  169. OptionStringPtr opt_basic1(new OptionString(Option::V4, 6, "BASIC.1"));
  170. option1->addOption(opt_server);
  171. option1->addOption(opt_basic1);
  172. response->addOption(option1);
  173. } else {
  174. LOG_INFO(runscript_logger, FLOWDAT_DEBUG_STRING).arg("cablemodem " + mac + " not found for MTA Activation");
  175. }
  176. }
  177. if (ht == EMTA) {
  178. std::string file = mac + ".mta.bin";
  179. isc::util::str::lowercase(file);
  180. std::map<std::string,std::string>::iterator it = option122_map.find(mac);
  181. }
  182. return 0;
  183. }
  184. // @TODO: Implementar para DHCPv6
  185. /* IPv6 callouts */
  186. int pkt6_receive(CalloutHandle& handle) {
  187. std::vector<std::string> env;
  188. Pkt6Ptr query;
  189. handle.getArgument("query6", query);
  190. // std::string mac = getMacFromPacket(query);
  191. // std::string class_map = conn->executeQuery("SELECT state FROM fd3_dhcp.host WHERE mac=" + string(mac));
  192. // cm_map[mac] = class_map;
  193. return 0;
  194. }
  195. } // end extern "C"