net_ipv6_wait_dynamic_addr.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, as formatted
  42. * by getnameinfo(..., NI_NUMERICHOST)
  43. * string prefix - prefix length
  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/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, o->ifaddr.addr, 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 (NCDModuleInst *i)
  84. {
  85. // allocate instance
  86. struct instance *o = malloc(sizeof(*o));
  87. if (!o) {
  88. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  89. goto fail0;
  90. }
  91. o->i = i;
  92. NCDModuleInst_Backend_SetUser(i, o);
  93. // read arguments
  94. NCDValRef ifname_arg;
  95. if (!NCDVal_ListRead(i->args, 1, &ifname_arg)) {
  96. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  97. goto fail1;
  98. }
  99. if (!NCDVal_IsStringNoNulls(ifname_arg)) {
  100. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  101. goto fail1;
  102. }
  103. const char *ifname = NCDVal_StringValue(ifname_arg);
  104. // get interface index
  105. int ifindex;
  106. if (!get_iface_info(ifname, NULL, NULL, &ifindex)) {
  107. ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
  108. goto fail1;
  109. }
  110. // init monitor
  111. if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_IPV6_ADDR, i->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) {
  112. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  113. goto fail1;
  114. }
  115. // set not up
  116. o->up = 0;
  117. return;
  118. fail1:
  119. free(o);
  120. fail0:
  121. NCDModuleInst_Backend_SetError(i);
  122. NCDModuleInst_Backend_Dead(i);
  123. }
  124. static void instance_free (struct instance *o)
  125. {
  126. NCDModuleInst *i = o->i;
  127. // free monitor
  128. NCDInterfaceMonitor_Free(&o->monitor);
  129. // free instance
  130. free(o);
  131. NCDModuleInst_Backend_Dead(i);
  132. }
  133. static void func_die (void *vo)
  134. {
  135. struct instance *o = vo;
  136. instance_free(o);
  137. }
  138. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  139. {
  140. struct instance *o = vo;
  141. ASSERT(o->up)
  142. if (!strcmp(name, "addr")) {
  143. char str[IPADDR6_PRINT_MAX];
  144. if (!ipaddr6_print_addr(o->ifaddr.addr, str)) {
  145. ModuleLog(o->i, BLOG_ERROR, "ipaddr6_print_addr failed");
  146. return 0;
  147. }
  148. *out = NCDVal_NewString(mem, str);
  149. if (NCDVal_IsInvalid(*out)) {
  150. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  151. }
  152. return 1;
  153. }
  154. if (!strcmp(name, "prefix")) {
  155. char str[10];
  156. sprintf(str, "%d", o->ifaddr.prefix);
  157. *out = NCDVal_NewString(mem, str);
  158. if (NCDVal_IsInvalid(*out)) {
  159. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  160. }
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. static const struct NCDModule modules[] = {
  166. {
  167. .type = "net.ipv6.wait_dynamic_addr",
  168. .func_new = func_new,
  169. .func_die = func_die,
  170. .func_getvar = func_getvar
  171. }, {
  172. .type = NULL
  173. }
  174. };
  175. const struct NCDModuleGroup ncdmodule_net_ipv6_wait_dynamic_addr = {
  176. .modules = modules
  177. };