net_ipv4_dhcp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * string server_mac - MAC address of the DHCP server (6 two-digit caps hexadecimal values
  38. * separated with colons, e.g."AB:CD:EF:01:02:03")
  39. */
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <stdio.h>
  43. #include <misc/debug.h>
  44. #include <misc/ipaddr.h>
  45. #include <dhcpclient/BDHCPClient.h>
  46. #include <ncd/NCDModule.h>
  47. #include <generated/blog_channel_ncd_net_ipv4_dhcp.h>
  48. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  49. struct instance {
  50. NCDModuleInst *i;
  51. BDHCPClient dhcp;
  52. int up;
  53. };
  54. static void instance_free (struct instance *o);
  55. static void dhcp_handler (struct instance *o, int event)
  56. {
  57. switch (event) {
  58. case BDHCPCLIENT_EVENT_UP: {
  59. ASSERT(!o->up)
  60. o->up = 1;
  61. NCDModuleInst_Backend_Up(o->i);
  62. } break;
  63. case BDHCPCLIENT_EVENT_DOWN: {
  64. ASSERT(o->up)
  65. o->up = 0;
  66. NCDModuleInst_Backend_Down(o->i);
  67. } break;
  68. case BDHCPCLIENT_EVENT_ERROR: {
  69. NCDModuleInst_Backend_SetError(o->i);
  70. instance_free(o);
  71. return;
  72. } break;
  73. default: ASSERT(0);
  74. }
  75. }
  76. static void func_new (NCDModuleInst *i)
  77. {
  78. // allocate instance
  79. struct instance *o = malloc(sizeof(*o));
  80. if (!o) {
  81. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  82. goto fail0;
  83. }
  84. NCDModuleInst_Backend_SetUser(i, o);
  85. // init arguments
  86. o->i = i;
  87. // check arguments
  88. NCDValue *arg;
  89. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  90. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  91. goto fail1;
  92. }
  93. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  94. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  95. goto fail1;
  96. }
  97. char *ifname = NCDValue_StringValue(arg);
  98. // init DHCP
  99. if (!BDHCPClient_Init(&o->dhcp, ifname, o->i->reactor, (BDHCPClient_handler)dhcp_handler, o)) {
  100. ModuleLog(o->i, BLOG_ERROR, "BDHCPClient_Init failed");
  101. goto fail1;
  102. }
  103. // set not up
  104. o->up = 0;
  105. return;
  106. fail1:
  107. free(o);
  108. fail0:
  109. NCDModuleInst_Backend_SetError(i);
  110. NCDModuleInst_Backend_Dead(i);
  111. }
  112. static void instance_free (struct instance *o)
  113. {
  114. NCDModuleInst *i = o->i;
  115. // free DHCP
  116. BDHCPClient_Free(&o->dhcp);
  117. // free instance
  118. free(o);
  119. NCDModuleInst_Backend_Dead(i);
  120. }
  121. static void func_die (void *vo)
  122. {
  123. struct instance *o = vo;
  124. instance_free(o);
  125. }
  126. static int func_getvar (void *vo, const char *name, NCDValue *out)
  127. {
  128. struct instance *o = vo;
  129. ASSERT(o->up)
  130. if (!strcmp(name, "addr")) {
  131. uint32_t addr;
  132. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  133. uint8_t *b = (uint8_t *)&addr;
  134. char str[50];
  135. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  136. if (!NCDValue_InitString(out, str)) {
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. if (!strcmp(name, "prefix")) {
  142. uint32_t addr;
  143. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  144. uint32_t mask;
  145. BDHCPClient_GetClientMask(&o->dhcp, &mask);
  146. struct ipv4_ifaddr ifaddr;
  147. if (!ipaddr_ipv4_ifaddr_from_addr_mask(addr, mask, &ifaddr)) {
  148. return 0;
  149. }
  150. char str[10];
  151. sprintf(str, "%d", ifaddr.prefix);
  152. if (!NCDValue_InitString(out, str)) {
  153. return 0;
  154. }
  155. return 1;
  156. }
  157. if (!strcmp(name, "gateway")) {
  158. uint32_t addr;
  159. if (!BDHCPClient_GetRouter(&o->dhcp, &addr)) {
  160. return 0;
  161. }
  162. uint8_t *b = (uint8_t *)&addr;
  163. char str[50];
  164. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  165. if (!NCDValue_InitString(out, str)) {
  166. return 0;
  167. }
  168. return 1;
  169. }
  170. if (!strcmp(name, "dns_servers")) {
  171. uint32_t servers[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
  172. int num_servers = BDHCPClient_GetDNS(&o->dhcp, servers, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
  173. NCDValue list;
  174. NCDValue_InitList(&list);
  175. for (int i = 0; i < num_servers; i++) {
  176. uint8_t *b = (uint8_t *)&servers[i];
  177. char str[50];
  178. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  179. NCDValue server;
  180. if (!NCDValue_InitString(&server, str)) {
  181. goto fail1;
  182. }
  183. if (!NCDValue_ListAppend(&list, server)) {
  184. NCDValue_Free(&server);
  185. goto fail1;
  186. }
  187. }
  188. *out = list;
  189. return 1;
  190. fail1:
  191. NCDValue_Free(&list);
  192. return 0;
  193. }
  194. if (!strcmp(name, "server_mac")) {
  195. uint8_t mac[6];
  196. BDHCPClient_GetServerMAC(&o->dhcp, mac);
  197. char str[18];
  198. sprintf(str, "%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8,
  199. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  200. if (!NCDValue_InitString(out, str)) {
  201. return 0;
  202. }
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. static const struct NCDModule modules[] = {
  208. {
  209. .type = "net.ipv4.dhcp",
  210. .func_new = func_new,
  211. .func_die = func_die,
  212. .func_getvar = func_getvar
  213. }, {
  214. .type = NULL
  215. }
  216. };
  217. const struct NCDModuleGroup ncdmodule_net_ipv4_dhcp = {
  218. .modules = modules
  219. };