net_ipv6_wait_dynamic_addr.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @file net_ipv6_wait_dynamic_addr.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. * Synopsis:
  32. * net.ipv6.wait_dynamic_addr(string ifname)
  33. *
  34. * Description:
  35. * Waits for a dynamic IPv6 address to be obtained on the interface,
  36. * and goes up when it is obtained.
  37. * If the address is subsequently lost, goes back down and again waits
  38. * for an address.
  39. *
  40. * Variables:
  41. * string addr - dynamic address obtained on the interface
  42. * string prefix - prefix length
  43. * string cidr_addr - address and prefix (addr/prefix)
  44. */
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <stdio.h>
  48. #include <misc/get_iface_info.h>
  49. #include <misc/ipaddr6.h>
  50. #include <ncd/NCDModule.h>
  51. #include <ncd/extra/NCDInterfaceMonitor.h>
  52. #include <generated/blog_channel_ncd_net_ipv6_wait_dynamic_addr.h>
  53. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  54. struct instance {
  55. NCDModuleInst *i;
  56. NCDInterfaceMonitor monitor;
  57. struct ipv6_ifaddr ifaddr;
  58. int up;
  59. };
  60. static void instance_free (struct instance *o);
  61. static void monitor_handler (struct instance *o, struct NCDInterfaceMonitor_event event)
  62. {
  63. if (!o->up && event.event == NCDIFMONITOR_EVENT_IPV6_ADDR_ADDED && (event.u.ipv6_addr.addr_flags & NCDIFMONITOR_ADDR_FLAG_DYNAMIC)) {
  64. // rememeber address, set up
  65. o->ifaddr = event.u.ipv6_addr.addr;
  66. o->up = 1;
  67. // signal up
  68. NCDModuleInst_Backend_Up(o->i);
  69. }
  70. else if (o->up && event.event == NCDIFMONITOR_EVENT_IPV6_ADDR_REMOVED && !memcmp(event.u.ipv6_addr.addr.addr.bytes, o->ifaddr.addr.bytes, 16) && event.u.ipv6_addr.addr.prefix == o->ifaddr.prefix) {
  71. // set not up
  72. o->up = 0;
  73. // signal down
  74. NCDModuleInst_Backend_Down(o->i);
  75. }
  76. }
  77. static void monitor_handler_error (struct instance *o)
  78. {
  79. ModuleLog(o->i, BLOG_ERROR, "monitor error");
  80. NCDModuleInst_Backend_SetError(o->i);
  81. instance_free(o);
  82. }
  83. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  84. {
  85. struct instance *o = vo;
  86. o->i = i;
  87. // read arguments
  88. NCDValRef ifname_arg;
  89. if (!NCDVal_ListRead(params->args, 1, &ifname_arg)) {
  90. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  91. goto fail0;
  92. }
  93. if (!NCDVal_IsStringNoNulls(ifname_arg)) {
  94. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  95. goto fail0;
  96. }
  97. // null terminate ifname
  98. NCDValNullTermString ifname_nts;
  99. if (!NCDVal_StringNullTerminate(ifname_arg, &ifname_nts)) {
  100. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  101. goto fail0;
  102. }
  103. // get interface index
  104. int ifindex;
  105. int res = get_iface_info(ifname_nts.data, NULL, NULL, &ifindex);
  106. NCDValNullTermString_Free(&ifname_nts);
  107. if (!res) {
  108. ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
  109. goto fail0;
  110. }
  111. // init monitor
  112. if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_IPV6_ADDR, i->params->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) {
  113. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  114. goto fail0;
  115. }
  116. // set not up
  117. o->up = 0;
  118. return;
  119. fail0:
  120. NCDModuleInst_Backend_SetError(i);
  121. NCDModuleInst_Backend_Dead(i);
  122. }
  123. static void instance_free (struct instance *o)
  124. {
  125. // free monitor
  126. NCDInterfaceMonitor_Free(&o->monitor);
  127. NCDModuleInst_Backend_Dead(o->i);
  128. }
  129. static void func_die (void *vo)
  130. {
  131. struct instance *o = vo;
  132. instance_free(o);
  133. }
  134. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  135. {
  136. struct instance *o = vo;
  137. ASSERT(o->up)
  138. if (!strcmp(name, "addr")) {
  139. char str[IPADDR6_PRINT_MAX];
  140. ipaddr6_print_addr(o->ifaddr.addr, str);
  141. *out = NCDVal_NewString(mem, str);
  142. if (NCDVal_IsInvalid(*out)) {
  143. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  144. }
  145. return 1;
  146. }
  147. if (!strcmp(name, "prefix")) {
  148. char str[10];
  149. sprintf(str, "%d", o->ifaddr.prefix);
  150. *out = NCDVal_NewString(mem, str);
  151. if (NCDVal_IsInvalid(*out)) {
  152. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  153. }
  154. return 1;
  155. }
  156. if (!strcmp(name, "cidr_addr")) {
  157. char str[IPADDR6_PRINT_MAX];
  158. ipaddr6_print_ifaddr(o->ifaddr, str);
  159. *out = NCDVal_NewString(mem, str);
  160. if (NCDVal_IsInvalid(*out)) {
  161. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  162. }
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. static struct NCDModule modules[] = {
  168. {
  169. .type = "net.ipv6.wait_dynamic_addr",
  170. .func_new2 = func_new,
  171. .func_die = func_die,
  172. .func_getvar = func_getvar,
  173. .alloc_size = sizeof(struct instance)
  174. }, {
  175. .type = NULL
  176. }
  177. };
  178. const struct NCDModuleGroup ncdmodule_net_ipv6_wait_dynamic_addr = {
  179. .modules = modules
  180. };