net_ipv4_dhcp.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * @file net_ipv4_dhcp.c
  3. * @author Ambroz Bizjak <[email protected]>
  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: net.ipv4.dhcp(string ifname, [list opts])
  34. * Description:
  35. * Runs a DHCP client on a network interface. When an address is obtained,
  36. * transitions up (but does not assign anything). If the lease times out,
  37. * transitions down.
  38. * The interface must already be up.
  39. * Supported options (in the opts argument):
  40. * - "hostname", (string value): send this hostname to the DHCP server
  41. * - "vendorclassid", (string value): send this vendor class identifier
  42. * - "auto_clientid": send a client identifier generated from the MAC address
  43. * Variables:
  44. * string addr - assigned IP address ("A.B.C.D")
  45. * string prefix - address prefix length ("N")
  46. * string gateway - router address ("A.B.C.D"), or "none" if not provided
  47. * list(string) dns_servers - DNS server addresses ("A.B.C.D" ...)
  48. * string server_mac - MAC address of the DHCP server (6 two-digit caps hexadecimal values
  49. * separated with colons, e.g."AB:CD:EF:01:02:03")
  50. */
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <stdio.h>
  54. #include <misc/debug.h>
  55. #include <misc/ipaddr.h>
  56. #include <dhcpclient/BDHCPClient.h>
  57. #include <ncd/NCDModule.h>
  58. #include <generated/blog_channel_ncd_net_ipv4_dhcp.h>
  59. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  60. struct instance {
  61. NCDModuleInst *i;
  62. BDHCPClient dhcp;
  63. int up;
  64. };
  65. static void instance_free (struct instance *o);
  66. static void dhcp_handler (struct instance *o, int event)
  67. {
  68. switch (event) {
  69. case BDHCPCLIENT_EVENT_UP: {
  70. ASSERT(!o->up)
  71. o->up = 1;
  72. NCDModuleInst_Backend_Up(o->i);
  73. } break;
  74. case BDHCPCLIENT_EVENT_DOWN: {
  75. ASSERT(o->up)
  76. o->up = 0;
  77. NCDModuleInst_Backend_Down(o->i);
  78. } break;
  79. case BDHCPCLIENT_EVENT_ERROR: {
  80. NCDModuleInst_Backend_SetError(o->i);
  81. instance_free(o);
  82. return;
  83. } break;
  84. default: ASSERT(0);
  85. }
  86. }
  87. static void func_new (NCDModuleInst *i)
  88. {
  89. // allocate instance
  90. struct instance *o = malloc(sizeof(*o));
  91. if (!o) {
  92. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  93. goto fail0;
  94. }
  95. NCDModuleInst_Backend_SetUser(i, o);
  96. // init arguments
  97. o->i = i;
  98. // check arguments
  99. NCDValRef ifname_arg;
  100. NCDValRef opts_arg = NCDVal_NewInvalid();
  101. if (!NCDVal_ListRead(i->args, 1, &ifname_arg) && !NCDVal_ListRead(i->args, 2, &ifname_arg, &opts_arg)) {
  102. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  103. goto fail1;
  104. }
  105. if (!NCDVal_IsStringNoNulls(ifname_arg) || (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsList(opts_arg))) {
  106. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  107. goto fail1;
  108. }
  109. const char *ifname = NCDVal_StringValue(ifname_arg);
  110. struct BDHCPClient_opts opts = {};
  111. // read options
  112. size_t count = NCDVal_IsInvalid(opts_arg) ? 0 : NCDVal_ListCount(opts_arg);
  113. for (size_t j = 0; j < count; j++) {
  114. NCDValRef opt = NCDVal_ListGet(opts_arg, j);
  115. // read name
  116. if (!NCDVal_IsStringNoNulls(opt)) {
  117. ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
  118. goto fail1;
  119. }
  120. const char *optname = NCDVal_StringValue(opt);
  121. if (!strcmp(optname, "hostname") || !strcmp(optname, "vendorclassid")) {
  122. // read value
  123. if (j == count) {
  124. ModuleLog(o->i, BLOG_ERROR, "option value missing");
  125. goto fail1;
  126. }
  127. NCDValRef val = NCDVal_ListGet(opts_arg, j + 1);
  128. if (!NCDVal_IsStringNoNulls(val)) {
  129. ModuleLog(o->i, BLOG_ERROR, "wrong option value type");
  130. goto fail1;
  131. }
  132. const char *optval = NCDVal_StringValue(val);
  133. if (!strcmp(optname, "hostname")) {
  134. opts.hostname = optval;
  135. } else {
  136. opts.vendorclassid = optval;
  137. }
  138. j++;
  139. }
  140. else if (!strcmp(optname, "auto_clientid")) {
  141. opts.auto_clientid = 1;
  142. }
  143. else {
  144. ModuleLog(o->i, BLOG_ERROR, "unknown option name");
  145. goto fail1;
  146. }
  147. }
  148. // init DHCP
  149. if (!BDHCPClient_Init(&o->dhcp, ifname, opts, o->i->iparams->reactor, (BDHCPClient_handler)dhcp_handler, o)) {
  150. ModuleLog(o->i, BLOG_ERROR, "BDHCPClient_Init failed");
  151. goto fail1;
  152. }
  153. // set not up
  154. o->up = 0;
  155. return;
  156. fail1:
  157. free(o);
  158. fail0:
  159. NCDModuleInst_Backend_SetError(i);
  160. NCDModuleInst_Backend_Dead(i);
  161. }
  162. static void instance_free (struct instance *o)
  163. {
  164. NCDModuleInst *i = o->i;
  165. // free DHCP
  166. BDHCPClient_Free(&o->dhcp);
  167. // free instance
  168. free(o);
  169. NCDModuleInst_Backend_Dead(i);
  170. }
  171. static void func_die (void *vo)
  172. {
  173. struct instance *o = vo;
  174. instance_free(o);
  175. }
  176. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  177. {
  178. struct instance *o = vo;
  179. ASSERT(o->up)
  180. if (!strcmp(name, "addr")) {
  181. uint32_t addr;
  182. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  183. uint8_t *b = (uint8_t *)&addr;
  184. char str[50];
  185. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  186. *out = NCDVal_NewString(mem, str);
  187. if (NCDVal_IsInvalid(*out)) {
  188. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  189. }
  190. return 1;
  191. }
  192. if (!strcmp(name, "prefix")) {
  193. uint32_t addr;
  194. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  195. uint32_t mask;
  196. BDHCPClient_GetClientMask(&o->dhcp, &mask);
  197. struct ipv4_ifaddr ifaddr;
  198. if (!ipaddr_ipv4_ifaddr_from_addr_mask(addr, mask, &ifaddr)) {
  199. ModuleLog(o->i, BLOG_ERROR, "bad netmask");
  200. return 0;
  201. }
  202. char str[10];
  203. sprintf(str, "%d", ifaddr.prefix);
  204. *out = NCDVal_NewString(mem, str);
  205. if (NCDVal_IsInvalid(*out)) {
  206. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  207. }
  208. return 1;
  209. }
  210. if (!strcmp(name, "gateway")) {
  211. char str[50];
  212. uint32_t addr;
  213. if (!BDHCPClient_GetRouter(&o->dhcp, &addr)) {
  214. strcpy(str, "none");
  215. } else {
  216. uint8_t *b = (uint8_t *)&addr;
  217. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  218. }
  219. *out = NCDVal_NewString(mem, str);
  220. if (NCDVal_IsInvalid(*out)) {
  221. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  222. }
  223. return 1;
  224. }
  225. if (!strcmp(name, "dns_servers")) {
  226. uint32_t servers[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
  227. int num_servers = BDHCPClient_GetDNS(&o->dhcp, servers, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
  228. *out = NCDVal_NewList(mem, num_servers);
  229. if (NCDVal_IsInvalid(*out)) {
  230. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewList failed");
  231. goto fail;
  232. }
  233. for (int i = 0; i < num_servers; i++) {
  234. uint8_t *b = (uint8_t *)&servers[i];
  235. char str[50];
  236. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  237. NCDValRef server = NCDVal_NewString(mem, str);
  238. if (NCDVal_IsInvalid(server)) {
  239. ModuleLog(o->i, BLOG_ERROR, "NCDVal_IsInvalid failed");
  240. goto fail;
  241. }
  242. NCDVal_ListAppend(*out, server);
  243. }
  244. return 1;
  245. }
  246. if (!strcmp(name, "server_mac")) {
  247. uint8_t mac[6];
  248. BDHCPClient_GetServerMAC(&o->dhcp, mac);
  249. char str[18];
  250. sprintf(str, "%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8":%02"PRIX8,
  251. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  252. *out = NCDVal_NewString(mem, str);
  253. if (NCDVal_IsInvalid(*out)) {
  254. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  255. }
  256. return 1;
  257. }
  258. return 0;
  259. fail:
  260. *out = NCDVal_NewInvalid();
  261. return 1;
  262. }
  263. static const struct NCDModule modules[] = {
  264. {
  265. .type = "net.ipv4.dhcp",
  266. .func_new = func_new,
  267. .func_die = func_die,
  268. .func_getvar = func_getvar
  269. }, {
  270. .type = NULL
  271. }
  272. };
  273. const struct NCDModuleGroup ncdmodule_net_ipv4_dhcp = {
  274. .modules = modules
  275. };