net_ipv6_wait_dynamic_addr.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/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. const char *ifname = NCDVal_StringValue(ifname_arg);
  98. // get interface index
  99. int ifindex;
  100. if (!get_iface_info(ifname, NULL, NULL, &ifindex)) {
  101. ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
  102. goto fail0;
  103. }
  104. // init monitor
  105. 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)) {
  106. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  107. goto fail0;
  108. }
  109. // set not up
  110. o->up = 0;
  111. return;
  112. fail0:
  113. NCDModuleInst_Backend_SetError(i);
  114. NCDModuleInst_Backend_Dead(i);
  115. }
  116. static void instance_free (struct instance *o)
  117. {
  118. // free monitor
  119. NCDInterfaceMonitor_Free(&o->monitor);
  120. NCDModuleInst_Backend_Dead(o->i);
  121. }
  122. static void func_die (void *vo)
  123. {
  124. struct instance *o = vo;
  125. instance_free(o);
  126. }
  127. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  128. {
  129. struct instance *o = vo;
  130. ASSERT(o->up)
  131. if (!strcmp(name, "addr")) {
  132. char str[IPADDR6_PRINT_MAX];
  133. ipaddr6_print_addr(o->ifaddr.addr, str);
  134. *out = NCDVal_NewString(mem, str);
  135. if (NCDVal_IsInvalid(*out)) {
  136. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  137. }
  138. return 1;
  139. }
  140. if (!strcmp(name, "prefix")) {
  141. char str[10];
  142. sprintf(str, "%d", o->ifaddr.prefix);
  143. *out = NCDVal_NewString(mem, str);
  144. if (NCDVal_IsInvalid(*out)) {
  145. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  146. }
  147. return 1;
  148. }
  149. if (!strcmp(name, "cidr_addr")) {
  150. char str[IPADDR6_PRINT_MAX];
  151. ipaddr6_print_ifaddr(o->ifaddr, str);
  152. *out = NCDVal_NewString(mem, str);
  153. if (NCDVal_IsInvalid(*out)) {
  154. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  155. }
  156. return 1;
  157. }
  158. return 0;
  159. }
  160. static struct NCDModule modules[] = {
  161. {
  162. .type = "net.ipv6.wait_dynamic_addr",
  163. .func_new2 = func_new,
  164. .func_die = func_die,
  165. .func_getvar = func_getvar,
  166. .alloc_size = sizeof(struct instance)
  167. }, {
  168. .type = NULL
  169. }
  170. };
  171. const struct NCDModuleGroup ncdmodule_net_ipv6_wait_dynamic_addr = {
  172. .modules = modules
  173. };