net_ipv4_dhcp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * @file net_ipv4_dhcp.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * DHCP client module.
  32. *
  33. * Synopsis:
  34. * net.ipv4.dhcp(string ifname [, list opts])
  35. *
  36. * Description:
  37. * Runs a DHCP client on a network interface. When an address is obtained,
  38. * transitions up (but does not assign anything). If the lease times out,
  39. * transitions down.
  40. * The interface must already be up.
  41. * Supported options (in the opts argument):
  42. * - "hostname", (string value): send this hostname to the DHCP server
  43. * - "vendorclassid", (string value): send this vendor class identifier
  44. * - "auto_clientid": send a client identifier generated from the MAC address
  45. *
  46. * Variables:
  47. * string addr - assigned IP address ("A.B.C.D")
  48. * string prefix - address prefix length ("N")
  49. * string cidr_addr - address and prefix in CIDR notation ("A.B.C.D/N")
  50. * string gateway - router address ("A.B.C.D"), or "none" if not provided
  51. * list(string) dns_servers - DNS server addresses ("A.B.C.D" ...)
  52. * string server_mac - MAC address of the DHCP server (6 two-digit caps hexadecimal values
  53. * separated with colons, e.g."AB:CD:EF:01:02:03")
  54. */
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <stdio.h>
  58. #include <misc/debug.h>
  59. #include <misc/ipaddr.h>
  60. #include <dhcpclient/BDHCPClient.h>
  61. #include <ncd/NCDModule.h>
  62. #include <generated/blog_channel_ncd_net_ipv4_dhcp.h>
  63. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  64. struct instance {
  65. NCDModuleInst *i;
  66. BDHCPClient dhcp;
  67. int up;
  68. };
  69. static void instance_free (struct instance *o, int is_error);
  70. static void dhcp_handler (struct instance *o, int event)
  71. {
  72. switch (event) {
  73. case BDHCPCLIENT_EVENT_UP: {
  74. ASSERT(!o->up)
  75. o->up = 1;
  76. NCDModuleInst_Backend_Up(o->i);
  77. } break;
  78. case BDHCPCLIENT_EVENT_DOWN: {
  79. ASSERT(o->up)
  80. o->up = 0;
  81. NCDModuleInst_Backend_Down(o->i);
  82. } break;
  83. case BDHCPCLIENT_EVENT_ERROR: {
  84. instance_free(o, 1);
  85. return;
  86. } break;
  87. default: ASSERT(0);
  88. }
  89. }
  90. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  91. {
  92. struct instance *o = vo;
  93. o->i = i;
  94. // check arguments
  95. NCDValRef ifname_arg;
  96. NCDValRef opts_arg = NCDVal_NewInvalid();
  97. if (!NCDVal_ListRead(params->args, 1, &ifname_arg) && !NCDVal_ListRead(params->args, 2, &ifname_arg, &opts_arg)) {
  98. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  99. goto fail0;
  100. }
  101. if (!NCDVal_IsStringNoNulls(ifname_arg) || (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsList(opts_arg))) {
  102. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  103. goto fail0;
  104. }
  105. NCDValNullTermString hostname_nts = NCDValNullTermString_NewDummy();
  106. NCDValNullTermString vendorclassid_nts = NCDValNullTermString_NewDummy();
  107. struct BDHCPClient_opts opts = {};
  108. // read options
  109. size_t count = NCDVal_IsInvalid(opts_arg) ? 0 : NCDVal_ListCount(opts_arg);
  110. for (size_t j = 0; j < count; j++) {
  111. NCDValRef opt = NCDVal_ListGet(opts_arg, j);
  112. // read name
  113. if (!NCDVal_IsString(opt)) {
  114. ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
  115. goto fail1;
  116. }
  117. if (NCDVal_StringEquals(opt, "hostname") || NCDVal_StringEquals(opt, "vendorclassid")) {
  118. int is_hostname = NCDVal_StringEquals(opt, "hostname");
  119. // read value
  120. if (j == count) {
  121. ModuleLog(o->i, BLOG_ERROR, "option value missing");
  122. goto fail1;
  123. }
  124. NCDValRef val = NCDVal_ListGet(opts_arg, j + 1);
  125. if (!NCDVal_IsStringNoNulls(val)) {
  126. ModuleLog(o->i, BLOG_ERROR, "wrong option value type");
  127. goto fail1;
  128. }
  129. // null terminate
  130. NCDValNullTermString nts;
  131. if (!NCDVal_StringNullTerminate(val, &nts)) {
  132. ModuleLog(o->i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  133. goto fail1;
  134. }
  135. NCDValNullTermString *nts_ptr = (is_hostname ? &hostname_nts : &vendorclassid_nts);
  136. NCDValNullTermString_Free(nts_ptr);
  137. *nts_ptr = nts;
  138. if (is_hostname) {
  139. opts.hostname = nts.data;
  140. } else {
  141. opts.vendorclassid = nts.data;
  142. }
  143. j++;
  144. }
  145. else if (NCDVal_StringEquals(opt, "auto_clientid")) {
  146. opts.auto_clientid = 1;
  147. }
  148. else {
  149. ModuleLog(o->i, BLOG_ERROR, "unknown option name");
  150. goto fail1;
  151. }
  152. }
  153. // null terminate ifname
  154. NCDValNullTermString ifname_nts;
  155. if (!NCDVal_StringNullTerminate(ifname_arg, &ifname_nts)) {
  156. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  157. goto fail1;
  158. }
  159. // init DHCP
  160. int res = BDHCPClient_Init(&o->dhcp, ifname_nts.data, opts, o->i->params->iparams->reactor, o->i->params->iparams->random2, (BDHCPClient_handler)dhcp_handler, o);
  161. NCDValNullTermString_Free(&ifname_nts);
  162. if (!res) {
  163. ModuleLog(o->i, BLOG_ERROR, "BDHCPClient_Init failed");
  164. goto fail1;
  165. }
  166. // set not up
  167. o->up = 0;
  168. // free options nts's
  169. NCDValNullTermString_Free(&hostname_nts);
  170. NCDValNullTermString_Free(&vendorclassid_nts);
  171. return;
  172. fail1:
  173. NCDValNullTermString_Free(&hostname_nts);
  174. NCDValNullTermString_Free(&vendorclassid_nts);
  175. fail0:
  176. NCDModuleInst_Backend_DeadError(i);
  177. }
  178. static void instance_free (struct instance *o, int is_error)
  179. {
  180. // free DHCP
  181. BDHCPClient_Free(&o->dhcp);
  182. if (is_error) {
  183. NCDModuleInst_Backend_DeadError(o->i);
  184. } else {
  185. NCDModuleInst_Backend_Dead(o->i);
  186. }
  187. }
  188. static void func_die (void *vo)
  189. {
  190. struct instance *o = vo;
  191. instance_free(o, 0);
  192. }
  193. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  194. {
  195. struct instance *o = vo;
  196. ASSERT(o->up)
  197. if (!strcmp(name, "addr")) {
  198. uint32_t addr;
  199. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  200. char str[IPADDR_PRINT_MAX];
  201. ipaddr_print_addr(addr, str);
  202. *out = NCDVal_NewString(mem, str);
  203. return 1;
  204. }
  205. if (!strcmp(name, "prefix")) {
  206. uint32_t addr;
  207. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  208. uint32_t mask;
  209. BDHCPClient_GetClientMask(&o->dhcp, &mask);
  210. struct ipv4_ifaddr ifaddr;
  211. if (!ipaddr_ipv4_ifaddr_from_addr_mask(addr, mask, &ifaddr)) {
  212. ModuleLog(o->i, BLOG_ERROR, "bad netmask");
  213. return 0;
  214. }
  215. char str[10];
  216. sprintf(str, "%d", ifaddr.prefix);
  217. *out = NCDVal_NewString(mem, str);
  218. return 1;
  219. }
  220. if (!strcmp(name, "cidr_addr")) {
  221. uint32_t addr;
  222. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  223. uint32_t mask;
  224. BDHCPClient_GetClientMask(&o->dhcp, &mask);
  225. struct ipv4_ifaddr ifaddr;
  226. if (!ipaddr_ipv4_ifaddr_from_addr_mask(addr, mask, &ifaddr)) {
  227. ModuleLog(o->i, BLOG_ERROR, "bad netmask");
  228. return 0;
  229. }
  230. char str[IPADDR_PRINT_MAX];
  231. ipaddr_print_ifaddr(ifaddr, str);
  232. *out = NCDVal_NewString(mem, str);
  233. return 1;
  234. }
  235. if (!strcmp(name, "gateway")) {
  236. char str[IPADDR_PRINT_MAX];
  237. uint32_t addr;
  238. if (!BDHCPClient_GetRouter(&o->dhcp, &addr)) {
  239. strcpy(str, "none");
  240. } else {
  241. ipaddr_print_addr(addr, str);
  242. }
  243. *out = NCDVal_NewString(mem, str);
  244. return 1;
  245. }
  246. if (!strcmp(name, "dns_servers")) {
  247. uint32_t servers[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
  248. int num_servers = BDHCPClient_GetDNS(&o->dhcp, servers, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
  249. *out = NCDVal_NewList(mem, num_servers);
  250. if (NCDVal_IsInvalid(*out)) {
  251. goto fail;
  252. }
  253. for (int i = 0; i < num_servers; i++) {
  254. char str[IPADDR_PRINT_MAX];
  255. ipaddr_print_addr(servers[i], str);
  256. NCDValRef server = NCDVal_NewString(mem, str);
  257. if (NCDVal_IsInvalid(server)) {
  258. goto fail;
  259. }
  260. if (!NCDVal_ListAppend(*out, server)) {
  261. goto fail;
  262. }
  263. }
  264. return 1;
  265. }
  266. if (!strcmp(name, "server_mac")) {
  267. uint8_t mac[6];
  268. BDHCPClient_GetServerMAC(&o->dhcp, mac);
  269. char str[18];
  270. sprintf(str, "%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8,
  271. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  272. *out = NCDVal_NewString(mem, str);
  273. return 1;
  274. }
  275. return 0;
  276. fail:
  277. *out = NCDVal_NewInvalid();
  278. return 1;
  279. }
  280. static struct NCDModule modules[] = {
  281. {
  282. .type = "net.ipv4.dhcp",
  283. .func_new2 = func_new,
  284. .func_die = func_die,
  285. .func_getvar = func_getvar,
  286. .alloc_size = sizeof(struct instance)
  287. }, {
  288. .type = NULL
  289. }
  290. };
  291. const struct NCDModuleGroup ncdmodule_net_ipv4_dhcp = {
  292. .modules = modules
  293. };