net_ipv4_dhcp.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @file net_ipv4_dhcp.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * DHCP client module.
  25. *
  26. * Synopsis: net.ipv4.dhcp(string ifname)
  27. * Description:
  28. * Runs a DHCP client on a network interface. When an address is obtained,
  29. * transitions up (but does not assign anything). If the lease times out,
  30. * transitions down.
  31. * The interface must already be up.
  32. * Variables:
  33. * string addr - assigned IP address ("A.B.C.D")
  34. * string prefix - address prefix length ("N")
  35. * string gateway - router address ("A.B.C.D")
  36. * list(string) dns_servers - DNS server addresses ("A.B.C.D" ...)
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #include <misc/debug.h>
  42. #include <misc/ipaddr.h>
  43. #include <dhcpclient/BDHCPClient.h>
  44. #include <ncd/NCDModule.h>
  45. #include <generated/blog_channel_ncd_net_ipv4_dhcp.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. struct instance {
  48. NCDModuleInst *i;
  49. BDHCPClient dhcp;
  50. int up;
  51. };
  52. static void instance_free (struct instance *o);
  53. static void dhcp_handler (struct instance *o, int event)
  54. {
  55. switch (event) {
  56. case BDHCPCLIENT_EVENT_UP: {
  57. ASSERT(!o->up)
  58. o->up = 1;
  59. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  60. } break;
  61. case BDHCPCLIENT_EVENT_DOWN: {
  62. ASSERT(o->up)
  63. o->up = 0;
  64. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  65. } break;
  66. case BDHCPCLIENT_EVENT_ERROR: {
  67. NCDModuleInst_Backend_SetError(o->i);
  68. instance_free(o);
  69. return;
  70. } break;
  71. default: ASSERT(0);
  72. }
  73. }
  74. static void func_new (NCDModuleInst *i)
  75. {
  76. // allocate instance
  77. struct instance *o = malloc(sizeof(*o));
  78. if (!o) {
  79. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  80. goto fail0;
  81. }
  82. NCDModuleInst_Backend_SetUser(i, o);
  83. // init arguments
  84. o->i = i;
  85. // check arguments
  86. NCDValue *arg;
  87. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  88. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  89. goto fail1;
  90. }
  91. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  92. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  93. goto fail1;
  94. }
  95. char *ifname = NCDValue_StringValue(arg);
  96. // init DHCP
  97. if (!BDHCPClient_Init(&o->dhcp, ifname, o->i->reactor, (BDHCPClient_handler)dhcp_handler, o)) {
  98. ModuleLog(o->i, BLOG_ERROR, "BDHCPClient_Init failed");
  99. goto fail1;
  100. }
  101. // set not up
  102. o->up = 0;
  103. return;
  104. fail1:
  105. free(o);
  106. fail0:
  107. NCDModuleInst_Backend_SetError(i);
  108. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  109. }
  110. static void instance_free (struct instance *o)
  111. {
  112. NCDModuleInst *i = o->i;
  113. // free DHCP
  114. BDHCPClient_Free(&o->dhcp);
  115. // free instance
  116. free(o);
  117. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  118. }
  119. static void func_die (void *vo)
  120. {
  121. struct instance *o = vo;
  122. instance_free(o);
  123. }
  124. static int func_getvar (void *vo, const char *name, NCDValue *out)
  125. {
  126. struct instance *o = vo;
  127. ASSERT(o->up)
  128. if (!strcmp(name, "addr")) {
  129. uint32_t addr;
  130. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  131. uint8_t *b = (uint8_t *)&addr;
  132. char str[50];
  133. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  134. if (!NCDValue_InitString(out, str)) {
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. if (!strcmp(name, "prefix")) {
  140. uint32_t addr;
  141. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  142. uint32_t mask;
  143. BDHCPClient_GetClientMask(&o->dhcp, &mask);
  144. struct ipv4_ifaddr ifaddr;
  145. if (!ipaddr_ipv4_ifaddr_from_addr_mask(addr, mask, &ifaddr)) {
  146. return 0;
  147. }
  148. char str[10];
  149. sprintf(str, "%d", ifaddr.prefix);
  150. if (!NCDValue_InitString(out, str)) {
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. if (!strcmp(name, "gateway")) {
  156. uint32_t addr;
  157. if (!BDHCPClient_GetRouter(&o->dhcp, &addr)) {
  158. return 0;
  159. }
  160. uint8_t *b = (uint8_t *)&addr;
  161. char str[50];
  162. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  163. if (!NCDValue_InitString(out, str)) {
  164. return 0;
  165. }
  166. return 1;
  167. }
  168. if (!strcmp(name, "dns_servers")) {
  169. uint32_t servers[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
  170. int num_servers = BDHCPClient_GetDNS(&o->dhcp, servers, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
  171. NCDValue list;
  172. NCDValue_InitList(&list);
  173. for (int i = 0; i < num_servers; i++) {
  174. uint8_t *b = (uint8_t *)&servers[i];
  175. char str[50];
  176. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  177. NCDValue server;
  178. if (!NCDValue_InitString(&server, str)) {
  179. goto fail1;
  180. }
  181. if (!NCDValue_ListAppend(&list, server)) {
  182. NCDValue_Free(&server);
  183. goto fail1;
  184. }
  185. }
  186. *out = list;
  187. return 1;
  188. fail1:
  189. NCDValue_Free(&list);
  190. return 0;
  191. }
  192. return 0;
  193. }
  194. static const struct NCDModule modules[] = {
  195. {
  196. .type = "net.ipv4.dhcp",
  197. .func_new = func_new,
  198. .func_die = func_die,
  199. .func_getvar = func_getvar
  200. }, {
  201. .type = NULL
  202. }
  203. };
  204. const struct NCDModuleGroup ncdmodule_net_ipv4_dhcp = {
  205. .modules = modules
  206. };